force1987

QuickSort

Apr 19th, 2021 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6. template <class T>
  7. void quickSortR(T a[], long N) {
  8. long i = 0, j = N;
  9. T p;
  10. p = a[N / 2];
  11. do {
  12. while (a[i] < p) i++;
  13. while (a[j] > p) j--;
  14. if (i <= j) {
  15. swap(a[i++], a[j--]);
  16. }
  17. } while (i <= j);
  18. if (j > 0) quickSortR(a, j);
  19. if (N > i) quickSortR(a + i, N - i);
  20. }
  21.  
  22. int main() {
  23. srand(time(NULL));
  24. const long SIZE = 10;
  25. int ar[SIZE];
  26. for (int i = 0; i < SIZE; i++) {
  27. ar[i] = rand() % 100;
  28. cout << ar[i] << "\t";
  29. }
  30. cout << "\n\n";
  31. quickSortR(ar, SIZE - 1);
  32. for (int i = 0; i < SIZE; i++) {
  33. cout << ar[i] << "\t";
  34. }
  35. cout <<endl;
  36. }
Add Comment
Please, Sign In to add comment