algo.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include"algo.h"
  2. #include<stdlib.h>
  3. #include<math.h>
  4. void QuickSort(int* arr, int left, int right)
  5. {
  6. if (left >= right)
  7. {
  8. return;
  9. }
  10. int position = PartSort(arr, left, right);
  11. QuickSort(arr, left, position - 1);
  12. QuickSort(arr, position + 1, right);
  13. }
  14. int PartSort(int* arr, int left, int right)
  15. {
  16. int key = arr[left];
  17. while (left < right) {
  18. while (left < right && arr[right] >= key)
  19. {
  20. right--;
  21. }
  22. swap(arr, left, right);
  23. while (left < right && arr[left] <= key)
  24. {
  25. left++;
  26. }
  27. swap(arr, left, right);
  28. }
  29. return left;
  30. }
  31. void swap(int* arr, int left, int right)
  32. {
  33. int temp = 0;
  34. temp = arr[right];
  35. arr[right] = arr[left];
  36. arr[left] = temp;
  37. }
  38. int* RandomArrayCreate(int Arraylength)
  39. {
  40. int static arr[10];
  41. srand((unsigned int)time(NULL));
  42. for (int i = 0; i < Arraylength; i++)
  43. {
  44. arr[i] = 1 + rand() % 100;
  45. }
  46. return arr;
  47. }