Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. #include "SortingClass.hpp"
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <iostream>
  6. #include <ctime>
  7. using namespace std;
  8.  
  9. SortingClass::SortingClass(int si, int sm, int la) {
  10. //(5 pts) random numbers should be between the smallest and largest numbers,
  11. //in-order should start at smallest
  12. //reverse order should start at largest
  13. //all should be of size size
  14. }
  15.  
  16. SortingClass::SortingClass(int si) {
  17. //(2 pts) largest should be 5000, smallest should be 10
  18. }
  19.  
  20. SortingClass::SortingClass() {
  21. // (2 pts)Start with a smaller size to make sure your sorting algorithms are working
  22. // and build up to 50000
  23. }
  24.  
  25. int *SortingClass::copyArr(string s) {
  26. //(7 pts) based on s (which can be “rev”, “ord”, or “rand”, creates a new array,
  27. // copies over the old array, and returns the address of the new array
  28. }
  29.  
  30. void SortingClass::selectionSort(int arr[]) {
  31. // (4 pts) Does what you’d think to the array passed into the method.
  32. int tmp;
  33.  
  34. for (int i = 0; i < 5; i++)
  35. cout << arr[i] << " ";
  36.  
  37. cout << endl;
  38.  
  39. for (int i = 0; i < 5 - 1; i++)
  40.  
  41. for (int j = i + 1; j < 5; j++)
  42.  
  43. if (arr[i] > arr[j]) {
  44. tmp = arr[i];
  45. arr[i] = arr[j];
  46. arr[j] = tmp;
  47. }
  48.  
  49. for (int i = 0; i < 5; i++)
  50. cout << arr[i] << " ";
  51.  
  52. cout << endl;
  53.  
  54. }
  55.  
  56. void SortingClass::insertionSort(int arr[]) {
  57. int i, j, tmp, length;
  58. length = (sizeof(arr)/sizeof(*arr));
  59.  
  60. for (i = 1; i < length; i++) {
  61. j = i;
  62. while (j > 0 && arr[j - 1] > arr[j]) {
  63. tmp = arr[j];
  64. arr[j] = arr[j - 1];
  65. arr[j - 1] = tmp;
  66. j--;
  67. }
  68. }
  69. }
  70.  
  71. void SortingClass::quickSort(int first, int last, int arr[]) {
  72. if (last - first > 1) { // There is data to be sorted.
  73. // Partition the table.
  74. int pivot = partition(first, last, arr);
  75.  
  76. // Sort the left half.
  77. quickSort(first, pivot - 1, arr);
  78.  
  79. // Sort the right half.
  80. quickSort(pivot + 1, last, arr);
  81. }
  82. }
  83.  
  84. int SortingClass::partition(int first, int last, int arr[]){
  85. int p = first;
  86. int pivot = arr[first];
  87. int i = first+1, j = last;
  88. int tmp;
  89. while (i <= j) {
  90. while (arr[i] < pivot)
  91. i++;
  92. while (arr[j] > pivot)
  93. j--;
  94. if (i <= j) {
  95. tmp = arr[i];
  96. arr[i] = arr[j];
  97. arr[j] = tmp;
  98. i++;
  99. j--;
  100. }
  101. }
  102. return p;
  103. }
  104.  
  105.  
  106.  
  107. void SortingClass::merge(int arr[], int f, int m, int l) {
  108. int n1 = m - f + 1;
  109. int n2 = l - m;
  110. int L[n1 + 1];
  111. int R[n2 + 1];
  112. for (int i = 1; i <= n1; i++) {
  113. L[i] = arr[f + i - 1];
  114. }
  115. for (int j = 1; j <= n2; j++) {
  116. R[j] = arr[m + j];
  117. }
  118. L[n1 + 1] = 999;
  119. R[n2 + 1] = 999;
  120. int i = 1, j = 1;
  121. for (int k = f; k <= l; k++) {
  122. if (L[i] <= R[j]) {
  123. arr[k] = L[i];
  124. i = i + 1;
  125. } else {
  126. arr[k] = R[j];
  127. j = j + 1;
  128. }
  129. }
  130. }
  131.  
  132. void SortingClass::mergeSort(int a[], int low, int hi) {
  133. // (4 pts)keeps dividing the portion of the array between the low index and the hi
  134. // index by dividing by 2
  135. int q;
  136. if (low < hi) {
  137. q = (low + hi) / 2;
  138. mergeSort(a, low, q);
  139. mergeSort(a, q + 1, hi);
  140. merge(a, low, q, hi);
  141. }
  142. }
  143.  
  144. void SortingClass::bubbleSort(int arr[], int n) {
  145. for (int i = 0; i < n; ++i)
  146. for (int j = 0; j < n - i - 1; ++j)
  147. if (arr[j] > arr[j + 1]) {
  148. int temp = arr[j];
  149. arr[j] = arr[j + 1];
  150. arr[j + 1] = temp;
  151. }
  152. }
  153.  
  154. void SortingClass::compareSorts() {
  155. clock_t startTime = clock();
  156. double timePassed;
  157. //SELECTION SORT
  158. int *arr = copyArr("rand");
  159. startTime = clock();
  160. selectionSort(arr);
  161. timePassed = clock() - startTime;
  162. for (int i = 0; i < size; i++) {
  163. cout << arr[i] << ", ";
  164. }
  165. cout << endl;
  166. delete [] arr;
  167. cout << "Selection: rand " << timePassed << endl;
  168. arr = copyArr("rev");
  169. startTime = clock();
  170. selectionSort(arr);
  171. timePassed = clock() - startTime;
  172. for (int i = 0; i < size; i++) {
  173. cout << arr[i] << ", ";
  174. }
  175. cout << endl;
  176. delete [] arr;
  177. cout << "Selection: rev " << timePassed << endl;
  178. arr = copyArr("ord");
  179. startTime = clock();
  180. selectionSort(arr);
  181. timePassed = clock() - startTime;
  182. for (int i = 0; i < size; i++) {
  183. cout << arr[i] << ", ";
  184. }
  185. cout << endl;
  186. delete [] arr;
  187. cout << "Selection: ord " << timePassed << endl;
  188. //INSERTION
  189. arr = copyArr("rand");
  190. startTime = clock();
  191. insertionSort(arr);
  192. timePassed = clock() - startTime;
  193. for (int i = 0; i < size; i++) {
  194. cout << arr[i] << ", ";
  195. }
  196. cout << endl;
  197. delete [] arr;
  198. cout << "Insertion: rand " << timePassed << endl;
  199. arr = copyArr("rev");
  200. startTime = clock();
  201. insertionSort(arr);
  202. timePassed = clock() - startTime;
  203. for (int i = 0; i < size; i++) {
  204. cout << arr[i] << ", ";
  205. }
  206. cout << endl;
  207. delete [] arr;
  208. cout << "Insertion: rev " << timePassed << endl;
  209. arr = copyArr("ord");
  210. startTime = clock();
  211. insertionSort(arr);
  212. timePassed = clock() - startTime;
  213. for (int i = 0; i < size; i++) {
  214. cout << arr[i] << ", ";
  215. }
  216. cout << endl;
  217. delete [] arr;
  218. cout << "Insertion: ord " << timePassed << endl;
  219. //QUICKSORT
  220. arr = copyArr("rand");
  221. startTime = clock();
  222. quickSort(0, size, arr);
  223. timePassed = clock() - startTime;
  224. for (int i = 0; i < size; i++) {
  225. cout << arr[i] << ", ";
  226. }
  227. cout << endl;
  228. delete [] arr;
  229. cout << "Quick: rand " << timePassed << endl;
  230. arr = copyArr("rev");
  231. startTime = clock();
  232. quickSort(0, size, arr);
  233. timePassed = clock() - startTime;
  234. for (int i = 0; i < size; i++) {
  235. cout << arr[i] << ", ";
  236. }
  237. cout << endl;
  238. delete [] arr;
  239. cout << "Quick: rev " << timePassed << endl;
  240. arr = copyArr("ord");
  241. startTime = clock();
  242. quickSort(0, size, arr);
  243. timePassed = clock() - startTime;
  244. for (int i = 0; i < size; i++) {
  245. cout << arr[i] << ", ";
  246. }
  247. cout << endl;
  248. delete [] arr;
  249. cout << "Quick: ord " << timePassed << endl;
  250. //MERGE
  251. arr = copyArr("rand");
  252. startTime = clock();
  253. mergeSort(arr, 0, size - 1);
  254. timePassed = clock() - startTime;
  255. for (int i = 0; i < size; i++) {
  256. cout << arr[i] << ", ";
  257. }
  258. cout << endl;
  259. delete [] arr;
  260. cout << "Merge: rand " << timePassed << endl;
  261. arr = copyArr("rev");
  262. startTime = clock();
  263. mergeSort(arr, 0, size - 1);
  264. timePassed = clock() - startTime;
  265. for (int i = 0; i < size; i++) {
  266. cout << arr[i] << ", ";
  267. }
  268. cout << endl;
  269. delete [] arr;
  270. cout << "Merge: rev " << timePassed << endl;
  271. arr = copyArr("ord");
  272. startTime = clock();
  273. mergeSort(arr, 0, size - 1);
  274. timePassed = clock() - startTime;
  275. for (int i = 0; i < size; i++) {
  276. cout << arr[i] << ", ";
  277. }
  278. cout << endl;
  279. delete [] arr;
  280. cout << "Merge: ord " << timePassed << endl;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement