Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 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. int main()
  16. {
  17. string fileName1 = "file1.txt";
  18. string fileName2 = "file2.txt";
  19. string fileName3 = "file3.txt";
  20. vector<int> file1vec;
  21. vector<int> file2vec;
  22. vector<int> file3vec;
  23. int count1 = 0;
  24. int count2 = 0;
  25. int count3 = 0;
  26. //1. Read in file1 from the download area containing integers in text format
  27. ifstream iDataFile;
  28. iDataFile.open(fileName1);
  29. if (iDataFile.fail())
  30. {
  31. cout << "Invalid file 1." << endl;
  32. system("PAUSE");
  33. return 1;
  34. }
  35. //2. Count the number of elements in the file
  36. while (!(iDataFile.eof()))
  37. {
  38. int data;
  39. iDataFile >> data;
  40. file1vec.push_back(data);
  41. count1++;
  42. }
  43. iDataFile.close();
  44. iDataFile.open(fileName2);
  45. if (iDataFile.fail())
  46. {
  47. cout << "Invalid file 2." << endl;
  48. system("PAUSE");
  49. return 1;
  50. }
  51. while (!(iDataFile.eof()))
  52. {
  53. int data;
  54. iDataFile >> data;
  55. file2vec.push_back(data);
  56. count2++;
  57. }
  58. iDataFile.close();
  59. iDataFile.open(fileName3);
  60. if (iDataFile.fail())
  61. {
  62. cout << "Invalid file 3." << endl;
  63. system("PAUSE");
  64. return 1;
  65. }
  66. while (!(iDataFile.eof()))
  67. {
  68. int data;
  69. iDataFile >> data;
  70. file3vec.push_back(data);
  71. count3++;
  72. }
  73. //3. Obtain the starting clock tick value for the Insertion Sort
  74. //4. Sort the file with the Insertion Sort
  75. //5. Obtain the ending clock tick value for the Insertion Sort
  76. //6. Obtain the starting clock tick value for the Shellsort
  77. //7. Sort the file with the Shellsort
  78. //8. Obtain the ending clock tick value for the Shellsort
  79. //9. Obtain the starting clock tick value for the Quicksort
  80. //10. Sort the file with the Quicksort
  81. //11. Obtain the ending clock tick value for the Quicksort
  82. //12. Display the statistical information for the file for each sort(see example below).
  83. //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.
  84. //14. Repeat these steps for file2 and file3
  85. system("PAUSE");
  86. return 0;
  87. }
  88.  
  89. void insertionSort(int a[], int l)
  90. {
  91. int j = 0;
  92. int temp;
  93.  
  94. for (int i = i; i < l; i++)
  95. {
  96. j = i;
  97.  
  98. while (j > 0 && a[j] < a[j - 1])
  99. {
  100. temp = a[j];
  101. a[j] = a[j - 1];
  102. a[j - 1] = temp;
  103. j--;
  104. }
  105. }
  106. }
  107.  
  108. void shellsort(int a[], int l)
  109. {
  110. int gap, i, j, temp;
  111. for (gap = l / 2; gap > 0; gap /= 2)
  112. {
  113. for (i = gap; i < l; i++)
  114. {
  115. for (j = i - gap; j >= 0 && a[j]>a[j + gap]; j -= gap)
  116. {
  117. temp = a[j];
  118. a[j] = a[j + gap];
  119. a[j + gap] = temp;
  120. }
  121. }
  122. }
  123. }
  124.  
  125. void quickSort(int a[], int start, int end)
  126. {
  127. int i = start, j = end;
  128. int temp;
  129. int pivot = a[(start + end) / 2];
  130.  
  131. while (i <= j)
  132. {
  133. while (a[i] < pivot)
  134. i++;
  135. while (a[j] > pivot)
  136. j--;
  137. if (i <= j)
  138. {
  139. temp = a[i];
  140. a[i] = a[j];
  141. a[j] = temp;
  142. i++;
  143. j--;
  144. }
  145. }
  146. if (start < j)
  147. quickSort(a, start, j);
  148. if (i < end)
  149. quickSort(a, i, end);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement