Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9.  
  10.  
  11.  
  12. template <typename T>
  13. int part(T & container,const int start,const int end)
  14. {
  15. auto pivot = container[end];
  16. auto partitionIndex = start;
  17.  
  18. for(int i = start; i < end; ++i)
  19. {
  20. if (container[i] <= pivot)
  21. {
  22. swap(container[i],container[partitionIndex]);
  23. partitionIndex++;
  24. }
  25. }
  26. swap(container[partitionIndex],container[end]);
  27.  
  28. return partitionIndex;
  29. }
  30.  
  31. template <typename T>
  32. void quickSort(T & container,const int start,const int end)
  33. {
  34. if(start < end)
  35. {
  36. auto partitionIndex = part(container, start, end);
  37. quickSort(container,start,partitionIndex-1);
  38. quickSort(container,partitionIndex+1,end);
  39.  
  40. }
  41. }
  42.  
  43. template <typename T>
  44. bool lessEqualSort(const T & container)
  45. {
  46. for(int i = 0; i < container.size(); ++i)
  47. {
  48. for(int j = i; j <container.size(); ++j)
  49. {
  50. if (container[i] > container[j])
  51. {
  52. return false;
  53. }
  54. }
  55. }
  56.  
  57. return true;
  58. }
  59.  
  60. int main()
  61. {
  62. const size_t SIZE{1000};
  63.  
  64. array<int,SIZE>a{0};
  65. for(int i = 0; i <= SIZE; ++i)
  66. {
  67. a[i] = a[i-1] +1;
  68. }
  69. random_shuffle(&a[0] , &a[1000]);
  70. int start{0};
  71. int end{999};
  72.  
  73. // vector<string>a{"Ryan","John","Daniel","Gavin","Frank","Danielle","Lucy","Sarah","Katie","Nicole"};
  74. // int start{0};
  75. // int end{9};
  76.  
  77. for (const auto element : a)
  78. {
  79. cout << element << " ";
  80. }
  81.  
  82. if(lessEqualSort(a)==true)
  83. {
  84. cout << endl;
  85. cout << "Sorted" << endl;
  86. }else
  87. {
  88. cout << endl;
  89. cout << "Unsorted" << endl;
  90. }
  91.  
  92.  
  93. quickSort(a, start, end);
  94.  
  95. cout << endl;
  96. cout << "------------------------------------------------------" << endl;
  97.  
  98.  
  99. for (const auto element : a)
  100. {
  101. cout << element << " ";
  102. }
  103.  
  104. if(lessEqualSort(a)==true)
  105. {
  106. cout << endl;
  107. cout << "Sorted" << endl;
  108. }else
  109. {
  110. cout << endl;
  111. cout << "Unsorted" << endl;
  112. }
  113.  
  114.  
  115. cout << endl;
  116. return 0;
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement