Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include "sorterhelper.h"
  4. using namespace std;
  5.  
  6. //prints content of vector
  7. void printMyVector(vector<int>myVector)
  8. {
  9. for(int i = 0; i < myVector.size(); i++)
  10. {
  11. cout << "index " << i << ": " << myVector[i] << endl;
  12. }
  13. }
  14.  
  15. int main()
  16. {
  17. cout << endl;
  18. //seeds random numbers
  19. srand(time(NULL));
  20.  
  21. //my testing data- size from 1-20
  22. vector<int>myVector;
  23. int a = (rand() % 10) + 1; //1 to 20
  24.  
  25. cout << "UNSORTED DATA: " << endl;
  26. //pushes back a ran num from 1-20
  27. for (int i =0; i < a; i++)
  28. {
  29. int b = (rand() % 1000) + 1; //1 to 20
  30. myVector.push_back(b);
  31. cout << "index " << i << ": " << myVector[i] << endl;
  32. }
  33.  
  34. cout << endl;
  35.  
  36. //calling bubbleSort
  37. cout << "bubble sort: " << endl;
  38. //bubbleSort(myVector);
  39. //printMyVector(myVector);
  40. cout << endl;
  41.  
  42. //calling insertionSort
  43. cout << "insertion sort: " << endl;
  44. //insertionSort(myVector);
  45. //printMyVector(myVector);
  46. cout << endl;
  47.  
  48. //calling mergeSort
  49. cout << "merge sort:" << endl;
  50. //mergeSort(myVector);
  51. //printMyVector( myVector);
  52. cout << endl;
  53.  
  54. //calling quickSort
  55. cout << "quick sort:" << endl;
  56. //quickSort(myVector);
  57. //printMyVector(myVector);
  58. cout << endl;
  59.  
  60. //calling selectionSort
  61. cout << "selection sort:" << endl;
  62. //selectionSort(myVector);
  63. //printMyVector(myVector);
  64. cout << endl;
  65.  
  66. return 0;
  67. }
  68. ///////////////////////
  69.  
  70.  
  71. #include <iostream>
  72. #include <vector>
  73.  
  74. #ifndef SORTINGFUN_SORTER_H
  75. #define SORTINGFUN_SORTER_H
  76.  
  77. template <typename T>
  78. class Sorter {
  79. protected:
  80. std::vector<T> data;
  81.  
  82. public:
  83. Sorter() {}
  84. Sorter(T& x):data(x) {}
  85. void setData(const std::vector<T>& x){
  86. data = x;
  87. }
  88. virtual void sort() = 0;
  89. };
  90.  
  91. //bubble sort stackoverflow
  92. template <typename T>
  93. class MysterySorterA : public Sorter<T> {
  94. public:
  95. virtual void sort()
  96. {/*
  97. //vector < int > myV;
  98. //myV = this - > data;
  99. bool swapp = true;
  100. while(swapp)
  101. {
  102. swapp = false;
  103. for (int i = 0; i < myVector.size()-1; i++)
  104. {
  105. if (myVector[i]>myVector[i+1] )
  106. {
  107. myVector[i] += myVector[i+1];
  108. myVector[i+1] = myVector[i] - myVector[i+1];
  109. myVector[i] -=myVector[i+1];
  110. swapp = true;
  111. }
  112. }
  113. }*/
  114. }
  115. };
  116.  
  117. //insertion from stanford
  118. template <typename T>
  119. class MysterySorterB : public Sorter<T> {
  120. public:
  121. virtual void sort()
  122. {/*
  123. std::cout << "Mystery Sorter B" << std::endl;
  124. for (int i = 1; i < myVector.size(); i++)
  125. {
  126. int temp = myVector[i];
  127.  
  128. int j = i;
  129. while (j >= 1 && myVector[j - 1] > temp)
  130. {
  131. myVector[j] = myVector[j - 1];
  132. j--;
  133. }
  134. myVector[j] = temp;
  135. }*/
  136. }
  137. };
  138.  
  139. //merge & mergeSort from stanford
  140. template <typename T>
  141. class MysterySorterC : public Sorter<T> {
  142. public:
  143. virtual void sort()
  144. {/*
  145. std::cout << "Mystery Sorter C" << std::endl;
  146.  
  147. void merge(vector<int> &myVector, vector<int> &v1, vector<int> &v2)
  148. {
  149. int n1 = v1.size();
  150. int n2 = v2.size();
  151. int p1 = 0;
  152. int p2 = 0;
  153. while (p1 < n1 && p2 < n2)
  154. {
  155. if (v1[p1] < v2[p2])
  156. {
  157. myVector.push_back(v1[p1++]);
  158. }
  159.  
  160. else
  161. {
  162. myVector.push_back(v2[p2++]);
  163. }
  164. }
  165.  
  166. while (p1 < n1)
  167. {
  168. myVector.push_back(v1[p1++]);
  169. }
  170.  
  171. while (p2 < n2)
  172. {
  173. myVector.push_back(v2[p2++]);
  174. }
  175. }
  176.  
  177. void mergeSort(vector<int> &myVector)
  178. {
  179. int n = myVector.size();
  180. if (n <= 1) return;
  181. vector<int> v1;
  182. vector<int> v2;
  183. for (int i=0; i < n; i++)
  184. {
  185. if (i < n / 2)
  186. {
  187. v1.push_back(myVector[i]);
  188. }
  189. else
  190. {
  191. v2.push_back(myVector[i]);
  192. }
  193. }
  194.  
  195. mergeSort(v1);
  196. mergeSort(v2);
  197. myVector.clear();
  198. merge(myVector, v1, v2);
  199. }*/
  200. }
  201. };
  202.  
  203. //quickSort from stanford
  204. template <typename T>
  205. class MysterySorterD : public Sorter<T> {
  206. public:
  207. virtual void sort()
  208. {/*
  209. std::cout << "Mystery Sorter D" << std::endl;
  210. //quickSort from stanford.edu
  211. int partition(vector<int> &myVector, int start, int finish)
  212. {
  213. int pivot = myVector[start];
  214. int lh = start + 1;
  215. int rh = finish;
  216.  
  217. while (true)
  218. {
  219. while (lh < rh && myVector[rh] >= pivot) rh--;
  220. while (lh < rh && myVector[lh] < pivot) lh++;
  221. if (lh == rh) break;
  222. // swap
  223. int tmp = myVector[lh];
  224. myVector[lh] = myVector[rh];
  225. myVector[rh] = tmp;
  226. }
  227.  
  228. if (myVector[lh] >= pivot) return start;
  229. myVector[start] = myVector[lh];
  230. myVector[lh] = pivot;
  231. return lh;
  232. }
  233.  
  234. void quickSort(vector<int> &myVector, int start, int finish)
  235. {
  236. if (start >= finish) return;
  237. int boundary = partition(myVector, start, finish);
  238. quickSort(myVector, start, boundary - 1);
  239. quickSort(myVector, boundary + 1, finish);
  240. }
  241.  
  242. void quickSort(vector<int> &myVector)
  243. {
  244. quickSort(myVector, 0, myVector.size() - 1);
  245. }*/
  246. }
  247. };
  248.  
  249. //selectionSort from stanford
  250. template <typename T>
  251. class MysterySorterE : public Sorter<T> {
  252. public:
  253. virtual void sort()
  254. {/*
  255. std::cout << "Mystery Sorter E" << std::endl;
  256.  
  257. for (int i = 0; i < v.size(); i++)
  258. {
  259. int smallestIndex = i;
  260. for (int j = i+1; j < v.size(); j++)
  261. {
  262. if (v[j] < v[smallestIndex])
  263. {
  264. smallestIndex = j;
  265. }
  266. }
  267. swap(v[i],v[smallestIndex]);
  268. }*/
  269. }
  270. };
  271.  
  272. #endif //SORTINGFUN_SORTER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement