fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int partition(vector<int>& arr, int low, int high) {
  4. int pivot = arr[high];
  5. int i = low - 1;
  6. for (int j = low; j <= high - 1; j++) {
  7. if (arr[j] < pivot) {
  8. i++;
  9. swap(arr[i], arr[j]);
  10. }
  11. }
  12. swap(arr[i + 1], arr[high]);
  13. return i + 1;
  14. }
  15.  
  16. void quickSort(vector<int>& arr, int low, int high) {
  17. if (low < high) {
  18. int pi = partition(arr, low, high);
  19. quickSort(arr, low, pi - 1);
  20. quickSort(arr, pi + 1, high);
  21. }
  22. }
  23.  
  24. int main() {
  25. vector<int> arr = {10, 7, 8, 9, 1, 5};
  26. int n = arr.size();
  27. clock_t begin = clock();
  28. quickSort(arr, 0, n - 1);
  29. for (int i = 0; i < n; i++) {
  30. cout << arr[i] << " ";
  31. }
  32. cout << "\n";
  33. clock_t end = clock();
  34. cout<<"Time run: "<<(float)(end-begin)/CLOCKS_PER_SEC<<" s"<<endl;
  35. }
  36.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
1 5 7 8 9 10 
Time run: 0 s