fork download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <functional>
  4.  
  5. void quickSort(std::vector<long>& numberList, long low, long high)
  6. {
  7. long pivot, indexLow, indexHigh, temp;
  8.  
  9. if(low<high){
  10. pivot = low;
  11. indexLow = low;
  12. indexHigh = high;
  13.  
  14. while(indexLow<indexHigh){
  15. while(numberList[indexLow] <= numberList[pivot]){
  16. indexLow++;
  17. }
  18. while(numberList[indexHigh] > numberList[pivot]){
  19. indexHigh--;
  20. }
  21.  
  22. if(indexLow<indexHigh){
  23. temp = numberList[indexLow];
  24. numberList[indexLow] = numberList[indexHigh];
  25. numberList[indexHigh] = temp;
  26. }
  27. }
  28.  
  29. temp = numberList[pivot];
  30. numberList[pivot] = numberList[indexHigh];
  31. numberList[indexHigh] = temp;
  32.  
  33. std::string s = std::accumulate(
  34. numberList.begin()+1, numberList.end(), std::to_string(numberList[0]),
  35. [](const std::string& a, auto& b) {
  36. return a + '-' + std::to_string(b);
  37. });
  38. printf("%s\n", s.c_str());
  39. printf("%d, %d, %d, %d, %d\n", low, indexLow, pivot, indexHigh, high);
  40. printf("left %d %d\n", low, indexHigh-1);
  41. quickSort(numberList, low, indexHigh-1);
  42. printf("right %d %d\n", indexHigh+1, high);
  43. quickSort(numberList, indexHigh+1, high);
  44. printf("end\n");
  45. }
  46. }
  47.  
  48. int main()
  49. {
  50. constexpr size_t count = 10;
  51. std::vector<long> arr(count);
  52. std::generate(arr.begin(), arr.end(), bind(std::uniform_int_distribution<>(0,count-1), std::mt19937()));
  53. quickSort(arr, 0, count-1);
  54. return std::is_sorted(arr.begin(), arr.end()) ? 0 : 1;
  55. }
  56.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
2-1-3-8-1-6-8-9-9-9
0, 7, 0, 6, 9
left 0 5
1-1-2-8-3-6-8-9-9-9
0, 3, 0, 2, 5
left 0 1
1-1-2-8-3-6-8-9-9-9
0, 2, 0, 1, 1
left 0 0
right 2 1
end
right 3 5
1-1-2-6-3-8-8-9-9-9
3, 7, 3, 5, 5
left 3 4
1-1-2-3-6-8-8-9-9-9
3, 5, 3, 4, 4
left 3 3
right 5 4
end
right 6 5
end
end
right 7 9
1-1-2-3-6-8-8-9-9-9
7, 11, 7, 9, 9
left 7 8
1-1-2-3-6-8-8-9-9-9
7, 11, 7, 8, 8
left 7 7
right 9 8
end
right 10 9
end
end