Guest User

Untitled

a guest
Nov 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <ctime>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. unsigned long comp = 0;
  11.  
  12. const int median3(vector<int> &a, int left, int right)
  13. {
  14. int center = (left + right)/2; //get middle index
  15.  
  16. // Put three element in order
  17. if(((a[left] < a[center])&&(a[center] < a[right])) || ((a[right] < a[center])&&(a[center] < a[left])))
  18. return center;
  19. else if(((a[center] < a[left])&&(a[left] < a[right])) || ((a[right] < a[left])&&(a[left] < a[center])))
  20. return left;
  21. else
  22. return right;
  23. }
  24.  
  25. void qsort(vector<int> &a, int left, int right)
  26. {
  27. //int pivot = a[left]; //choose 1st element as pivot
  28.  
  29. //int pivot = a[right - 1]; //choose last element as pivot
  30. //swap(a[left], a[right - 1]);
  31.  
  32. int median_of_three = median3(a, left, right - 1);
  33. //choose median element as pivot
  34. int pivot = a[median_of_three];
  35. swap(a[left], a[median_of_three]);
  36.  
  37. int begin = left, end = right;
  38.  
  39. int i = begin + 1, j = begin + 1;
  40.  
  41. if((begin + 1) == end)
  42. return;
  43.  
  44. for(; j < end; j++)
  45. {
  46. if(a[j] < pivot)
  47. {
  48. swap(a[i], a[j]);
  49. i++;
  50. }
  51. }
  52. swap(a[begin], a[i-1]);
  53.  
  54. comp += end - (begin + 1);
  55.  
  56. //test the value of i
  57. //cout << "i = " << i << endl;
  58. if(i > (begin + 1))
  59. qsort(a, begin, i-1);
  60. if(i < end)
  61. qsort(a, i, end);
  62. }
  63.  
  64. int main(void)
  65. {
  66. const clock_t begin_time = clock(); // begin time
  67. ifstream myfile;
  68.  
  69. myfile.open("QuickSort.txt");
  70. if(!myfile)
  71. {
  72. cerr << "Cannot open file!\n";
  73. exit(1);
  74. }
  75.  
  76. //test comp
  77. cout << "Comp = " << comp << endl;
  78. // Read data
  79. vector<int> Input;
  80. string temp;
  81.  
  82. while(!myfile.eof())
  83. {
  84. getline(myfile, temp);
  85. if(temp.size() == 0)
  86. {
  87. continue;
  88. }
  89. Input.push_back(atoi(temp.c_str()));
  90. }
  91.  
  92. myfile.close();
  93.  
  94.  
  95. //cout << double(begin_time) << endl;
  96.  
  97. int length = Input.size();
  98. cout<<length<<" numbers acquired.\n";
  99.  
  100. // Quick sort for input array
  101. qsort(Input, 0, length);
  102. cout << "Time:(ms) " << double(clock() - begin_time) / CLOCKS_PER_SEC * 1000.0 << endl;
  103. //cout << "1st: " << Input[0] << endl;
  104. cout << "Comp = " << comp << endl;
  105.  
  106. //test
  107. vector<int> mytest;
  108. mytest.push_back(3);
  109. mytest.push_back(1);
  110. mytest.push_back(2);
  111.  
  112. //qsort(mytest, 0, 3);
  113. //cout << "After: " << mytest[0] << endl;
  114. //cout << "Comp = " << comp << endl;
  115.  
  116. return 0;
  117. }
Add Comment
Please, Sign In to add comment