Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. const int n = 25000;
  7. vector<string> selectionSorted(int n, vector<string>);
  8. vector<string> mergeSort(vector<string> &L, int a, int b);
  9. vector<string> merge(const vector<string> &left, const vector<string> &right);
  10. void quickSort(std::vector<string> &L, int a, int b);
  11.  
  12.  
  13. int main() {
  14. ifstream inFile;
  15.  
  16. inFile.open("shuffled.txt");
  17.  
  18. vector<string> words;
  19. string line;
  20.  
  21.  
  22. while (inFile >> line)
  23. {
  24. words.push_back(line);
  25. }
  26. inFile.close();
  27.  
  28.  
  29.  
  30. cout << "Original List: ";
  31. for(int i = 0; i < words.size(); i++)
  32. {
  33. cout << words[i] << " ";
  34. }
  35. cout << endl;
  36.  
  37. //selection sort
  38. selectionSorted(n,words);
  39.  
  40.  
  41. words.clear();
  42.  
  43.  
  44.  
  45. inFile.open("shuffled.txt");
  46. while (inFile >> line) {
  47. words.push_back(line);
  48. }
  49. cout << words.size() << endl;
  50. inFile.close();
  51.  
  52.  
  53. //merge sort
  54. //mergeSort(words);
  55. //auto sorted = mergeSort(words,0,0);
  56. mergeSort(words,0,-1);
  57.  
  58. vector<string> &sorted1 = words;
  59.  
  60. cout << "Merge Sort Sorted: ";
  61. for(int i = 0; i < words.size(); i++)
  62. {
  63. cout << words[i] << " ";
  64. }
  65. cout << endl;
  66.  
  67.  
  68. //quick sort
  69. int l,r,i,n;
  70. quickSort(words,0,n - 1);
  71. cout << "Quick Sort Sorted: ";
  72. for(i = 0; i < words.size(); i++)
  73. {
  74. cout << words[i] << " ";
  75. }
  76. cout << endl;
  77. //getch();
  78.  
  79.  
  80. return 0;
  81. }
  82.  
  83. vector<string> selectionSorted(int n, vector<string> words)
  84. {
  85. for (int pass = 0; pass <= n - 2; ++pass)
  86. {
  87.  
  88. int minIndex = pass;
  89. for (int i = minIndex + 1; i < n; i++)
  90. {
  91. if (words[i] < words[minIndex])
  92. {
  93. minIndex = i;
  94.  
  95. }
  96. }
  97. swap(words[pass], words[minIndex]);
  98. }
  99. cout << "Selection sort sorted: ";
  100. for (auto x : words)
  101. cout << x << " ";
  102. cout << "\n";
  103.  
  104. return words;
  105. }
  106.  
  107. vector<string> mergeSort(vector<string> &L, int a, int b)
  108. {
  109. if (b == -1)
  110. b = L.size();
  111.  
  112. const int k = b - a;
  113.  
  114.  
  115. if (k == 1)
  116. return { L[a] };
  117. else if (k == 0)
  118. return { };
  119.  
  120. int m = (a + b) / 2;
  121. vector<string> left = mergeSort(L, a, m), right = mergeSort(L, m, b);
  122.  
  123. return merge(left, right);
  124. }
  125.  
  126. vector<string> merge(const vector<string> &left, const vector<string> &right) {
  127. int l = 0, r = 0;
  128.  
  129. vector<string> v;
  130. v.reserve(left.size() + right.size());
  131. while (l < left.size() || r < right.size())
  132. {
  133. if (r >= right.size() || (l < left.size() && left[l] <= right[r]))
  134. {
  135. v.push_back(left[l]);
  136. ++l;
  137. }
  138. else
  139. {
  140. v.push_back(right[r]);
  141. ++r;
  142. }
  143. }
  144.  
  145. return v;
  146. }
  147.  
  148. void quickSort(std::vector<string> &L, int a, int b)
  149. {
  150. if (b == -1)
  151. b = L.size();
  152.  
  153. const int k = b - a;
  154.  
  155. // If the list is empty or has just one element, it's already sorted
  156. if (k <= 1)
  157. return;
  158.  
  159. // Now, the actual quick sort —
  160.  
  161. // Select a pivot
  162. // For now, use the first element as the pivot
  163.  
  164. // piv: index of the pivot element
  165. int piv = a;
  166.  
  167. // Pivot the list (move elements less than the pivot to before it, and ones
  168. // greater than the pivot after it)
  169. swap(L[piv], L[a]); // move pivot to first element
  170. piv = a; // since I moved the pivot, update piv
  171.  
  172. for (int i = piv + 1; i < b; ++i)
  173. {
  174.  
  175.  
  176. // If it's ≥, we don't need to do anything because it's already on the
  177. // right side of the pivot
  178. if (L[i] < L[piv])
  179. {
  180. // Swap with element to the right of the pivot
  181. swap(L[i], L[piv + 1]);
  182. // Swap it with the pivot
  183. swap(L[piv], L[piv + 1]);
  184. ++piv; // moved the pivot one to the right
  185. }
  186. }
  187.  
  188. // done pivoting — next step is to sort each half
  189. quickSort(L, a, piv);
  190. quickSort(L, piv + 1, b);
  191.  
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement