Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. //Keith Perkins
  2. //CS 2420
  3. //Program 6
  4. //Sorting
  5. #include <string>
  6. #include <vector>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <time.h>
  10. #include <stdio.h>
  11. #include <dos.h>
  12. #include "stdafx.h"
  13. using namespace std;
  14.  
  15. void insertionSort(int a[], int l);
  16. void shellSort(int a[], int l);
  17. void quickSort(int a[], int start, int end);
  18. int main()
  19. {
  20. clock_t clock();
  21. clock_t start;
  22. clock_t end;
  23. clock_t elapsed_clock;
  24. clock_t elapsed_time;
  25. string fileName1 = "file1.txt";
  26. string fileName2 = "file2.txt";
  27. string fileName3 = "file3.txt";
  28. vector<int> file1vec;
  29. vector<int> file2vec;
  30. vector<int> file3vec;
  31. int count1 = 0;
  32. int count2 = 0;
  33. int count3 = 0;
  34. //1. Read in file1 from the download area containing integers in text format
  35. ifstream iDataFile;
  36. iDataFile.open(fileName1);
  37. if (iDataFile.fail())
  38. {
  39. cout << "Invalid file 1." << endl;
  40. system("PAUSE");
  41. return 1;
  42. }
  43. //2. Count the number of elements in the file
  44. while (!(iDataFile.eof()))
  45. {
  46. int data;
  47. iDataFile >> data;
  48. file1vec.push_back(data);
  49. count1++;
  50. }
  51. int* file1arr = new int[count1];
  52. for (int i = 0; i < count1; i++)
  53. {
  54. file1arr[i] = file1vec.at(i);
  55. }
  56. iDataFile.close();
  57. iDataFile.open(fileName2);
  58. if (iDataFile.fail())
  59. {
  60. cout << "Invalid file 2." << endl;
  61. system("PAUSE");
  62. return 1;
  63. }
  64. while (!(iDataFile.eof()))
  65. {
  66. int data;
  67. iDataFile >> data;
  68. file2vec.push_back(data);
  69. count2++;
  70. }
  71. int* file2arr = new int[count2];
  72. for (int i = 0; i < count2; i++)
  73. {
  74. file2arr[i] = file2vec.at(i);
  75. }
  76. iDataFile.close();
  77. iDataFile.open(fileName3);
  78. if (iDataFile.fail())
  79. {
  80. cout << "Invalid file 3." << endl;
  81. system("PAUSE");
  82. return 1;
  83. }
  84. while (!(iDataFile.eof()))
  85. {
  86. int data;
  87. iDataFile >> data;
  88. file3vec.push_back(data);
  89. count3++;
  90. }
  91. int* file3arr = new int[count3];
  92. for (int i = 0; i < count3; i++)
  93. {
  94. file3arr[i] = file3vec.at(i);
  95. }
  96.  
  97. start = clock();
  98. insertionSort(file1arr, count1);
  99. end = clock();
  100. elapsed_clock = end - start;
  101. elapsed_time = ((end - start) / CLK_TCK);
  102. cout << "The elapsed clock was: " << elapsed_clock << endl;
  103. cout << "The elapsed time was: " << elapsed_time << endl;
  104. ofstream oDataFile;
  105. oDataFile.open("I1.txt");
  106. for (int i = 0; i < count1; i++)
  107. oDataFile << file1arr[i] << endl;
  108. oDataFile.close();
  109.  
  110. start = clock();
  111. shellSort(file1arr, count1);
  112. end = clock();
  113. elapsed_clock = end - start;
  114. elapsed_time = ((end - start) / CLK_TCK);
  115. cout << "The elapsed clock was: " << elapsed_clock << endl;
  116. cout << "The elapsed time was: " << elapsed_time << endl;
  117. oDataFile.open("S1.txt");
  118. for (int i = 0; i < count1; i++)
  119. oDataFile << file1arr[i] << endl;
  120. oDataFile.close();
  121.  
  122. start = clock();
  123. quickSort(file1arr, 0, count1);
  124. end = clock();
  125. elapsed_clock = end - start;
  126. elapsed_time = ((end - start) / CLK_TCK);
  127. cout << "The elapsed clock was: " << elapsed_clock << endl;
  128. cout << "The elapsed time was: " << elapsed_time << endl;
  129. oDataFile.open("Q1.txt");
  130. for (int i = 0; i < count1; i++)
  131. oDataFile << file1arr[i] << endl;
  132. oDataFile.close();
  133.  
  134. start = clock();
  135. insertionSort(file2arr, count2);
  136. end = clock();
  137. elapsed_clock = end - start;
  138. elapsed_time = ((end - start) / CLK_TCK);
  139. cout << "The elapsed clock was: " << elapsed_clock << endl;
  140. cout << "The elapsed time was: " << elapsed_time << endl;
  141. oDataFile.open("I2.txt");
  142. for (int i = 0; i < count2; i++)
  143. oDataFile << file2arr[i] << endl;
  144. oDataFile.close();
  145.  
  146. start = clock();
  147. shellSort(file2arr, count2);
  148. end = clock();
  149. elapsed_clock = end - start;
  150. elapsed_time = ((end - start) / CLK_TCK);
  151. cout << "The elapsed clock was: " << elapsed_clock << endl;
  152. cout << "The elapsed time was: " << elapsed_time << endl;
  153. oDataFile.open("S2.txt");
  154. for (int i = 0; i < count2; i++)
  155. oDataFile << file2arr[i] << endl;
  156. oDataFile.close();
  157.  
  158. start = clock();
  159. quickSort(file2arr, 0, count2);
  160. end = clock();
  161. elapsed_clock = end - start;
  162. elapsed_time = ((end - start) / CLK_TCK);
  163. cout << "The elapsed clock was: " << elapsed_clock << endl;
  164. cout << "The elapsed time was: " << elapsed_time << endl;
  165. oDataFile.open("Q2.txt");
  166. for (int i = 0; i < count2; i++)
  167. oDataFile << file2arr[i] << endl;
  168. oDataFile.close();
  169.  
  170. //3. Obtain the starting clock tick value for the Insertion Sort
  171. //4. Sort the file with the Insertion Sort
  172. //5. Obtain the ending clock tick value for the Insertion Sort
  173. //6. Obtain the starting clock tick value for the Shellsort
  174. //7. Sort the file with the Shellsort
  175. //8. Obtain the ending clock tick value for the Shellsort
  176. //9. Obtain the starting clock tick value for the Quicksort
  177. //10. Sort the file with the Quicksort
  178. //11. Obtain the ending clock tick value for the Quicksort
  179. //12. Display the statistical information for the file for each sort(see example below).
  180. //13. Write the sorted file out to the disk with a new name with the first letter of the sort(i.e.I for the Insertion sort, S for the Shellsort, or Q for the Quicksort) followed by the number of the file(i.e.I1, I2, I3 for the Insertion Sorted files, S1, S2, S3 for the Shellsort, and Q1, Q2, Q3 for the Quicksort).Each number should be written as text with each number on a separate line in the file.
  181. //14. Repeat these steps for file2 and file3
  182. system("PAUSE");
  183. return 0;
  184. }
  185.  
  186. void insertionSort(int a[], int l)
  187. {
  188. int j = 0;
  189. int temp;
  190.  
  191. for (int i = 1; i < l; i++)
  192. {
  193. j = i;
  194.  
  195. while (j > 0 && a[j] < a[j - 1])
  196. {
  197. temp = a[j];
  198. a[j] = a[j - 1];
  199. a[j - 1] = temp;
  200. j--;
  201. }
  202. }
  203. }
  204.  
  205. //void shellsort(int a[], int l)
  206. //{
  207. // int gap, i, j, temp;
  208. // for (gap = l / 2; gap > 0; gap /= 2)
  209. // {
  210. // for (i = gap; i < l; i++)
  211. // {
  212. // for (j = i - gap; j >= 0 && a[j]>a[j + gap]; j -= gap)
  213. // {
  214. // temp = a[j];
  215. // a[j] = a[j + gap];
  216. // a[j + gap] = temp;
  217. // }
  218. // }
  219. // }
  220. //}
  221.  
  222. void shellSort(int v[], int n)
  223.  
  224. {
  225.  
  226. int gap, i, j, temp;
  227.  
  228.  
  229.  
  230. for (gap = n / 2; gap > 0; gap /= 2)
  231.  
  232. for (i = gap; i < n; i++)
  233.  
  234. for (j = i - gap; j >= 0 && v[j]>v[j + gap]; j -= gap) {
  235.  
  236. temp = v[j];
  237.  
  238. v[j] = v[j + gap];
  239.  
  240. v[j + gap] = temp;
  241.  
  242. }
  243.  
  244. }
  245.  
  246. //void quickSort(int a[], int start, int end)
  247. //{
  248. // int i = start, j = end;
  249. // int temp;
  250. // int pivot = a[(start + end) / 2];
  251. //
  252. // while (i <= j)
  253. // {
  254. // while (a[i] < pivot)
  255. // i++;
  256. // while (a[j] > pivot)
  257. // j--;
  258. // if (i <= j)
  259. // {
  260. // temp = a[i];
  261. // a[i] = a[j];
  262. // a[j] = temp;
  263. // i++;
  264. // j--;
  265. // }
  266. // }
  267. // if (start < j)
  268. // quickSort(a, start, j);
  269. // if (i < end)
  270. // quickSort(a, i, end);
  271. //}
  272.  
  273. void quickSort(int arr[], int left, int right)
  274. {
  275. int i = left, j = right;
  276. int tmp;
  277. int pivot = arr[(left + right) / 2];
  278.  
  279. /* partition */
  280. while (i <= j) {
  281. while (arr[i] < pivot)
  282. i++;
  283. while (arr[j] > pivot)
  284. j--;
  285. if (i <= j) {
  286. tmp = arr[i];
  287. arr[i] = arr[j];
  288. arr[j] = tmp;
  289. i++;
  290. j--;
  291. }
  292. }
  293. /* recursion */
  294. if (left < j)
  295. quickSort(arr, left, j);
  296. if (i < right)
  297. quickSort(arr, i, right);
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement