algo.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. }
  39. int* RandomArrayCreate(int Arraylength)
  40. {
  41. int static arr[10];
  42. srand((unsigned int)time(NULL));
  43. for (int i = 0; i < Arraylength; i++)
  44. {
  45. arr[i] = 1 + rand() % 100;
  46. }
  47. return arr;
  48. }