fork download
  1. #include <stdio.h>
  2.  
  3. #define N 20
  4.  
  5. // 快速排序函数
  6. void QuickSort(int a[], int low, int high)
  7. {
  8. int i, j, pivot, temp;
  9.  
  10. if(low >= high)
  11. return;
  12.  
  13. i = low;
  14. j = high;
  15. pivot = a[low];
  16.  
  17. while(i < j)
  18. {
  19. while(i < j && a[j] >= pivot)
  20. j--;
  21.  
  22. while(i < j && a[i] <= pivot)
  23. i++;
  24.  
  25. if(i < j)
  26. {
  27. temp = a[i];
  28. a[i] = a[j];
  29. a[j] = temp;
  30. }
  31. }
  32.  
  33. a[low] = a[i];
  34. a[i] = pivot;
  35.  
  36. QuickSort(a, low, i - 1);
  37. QuickSort(a, i + 1, high);
  38. }
  39.  
  40. int main()
  41. {
  42. int a[N] = {
  43. 45, 12, 78, 3, 56,
  44. 29, 91, 17, 64, 8,
  45. 33, 70, 1, 52, 86,
  46. 24, 39, 95, 11, 60
  47. };
  48.  
  49. int i;
  50.  
  51. printf("排序前的数据:\n");
  52. for(i = 0; i < N; i++)
  53. printf("%d ", a[i]);
  54.  
  55. // 调用快速排序
  56. QuickSort(a, 0, N - 1);
  57.  
  58. printf("\n\n快速排序后的数据:\n");
  59. for(i = 0; i < N; i++)
  60. printf("%d ", a[i]);
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
排序前的数据:
45 12 78 3 56 29 91 17 64 8 33 70 1 52 86 24 39 95 11 60 

快速排序后的数据:
1 3 8 11 12 17 24 29 33 39 45 52 56 60 64 70 78 86 91 95