algo.cpp 867 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include"algo.h"
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<time.h>
  5. void QuickSort(int* arr, int left, int right)
  6. {
  7. if (left >= right)
  8. {
  9. return;
  10. }
  11. int position = PartSort(arr, left, right);
  12. QuickSort(arr, left, position - 1);
  13. QuickSort(arr, position + 1, right);
  14. }
  15. int PartSort(int* arr, int left, int right)
  16. {
  17. int key = arr[left];
  18. while (left < right) {
  19. while (left < right && arr[right] >= key)
  20. {
  21. right--;
  22. }
  23. swap(arr, left, right);
  24. while (left < right && arr[left] <= key)
  25. {
  26. left++;
  27. }
  28. swap(arr, left, right);
  29. }
  30. return left;
  31. }
  32. void swap(int* arr, int left, int right)
  33. {
  34. int temp = 0;
  35. temp = arr[right];
  36. arr[right] = arr[left];
  37. arr[left] = temp;
  38. }