快速排序(quickSort)

   1. #include <stdio.h>  
   2. int t, i, j, n, temp, seq[100];  
   3.   
   4. int patition(int start, int end)  
   5. {  
   6.     temp = seq[end];      
   7.     j = end;  
   8.     i = start;  
   9.     while (j > i)  
  10.     {  
  11.         while (j > i && seq[i] <= temp) i++;  
  12.         seq[j] = seq[i];  
  13.         while (j > i && seq[j] >= temp) j--;  
  14.         seq[i] = seq[j];  
  15.     }  
  16.     seq[j] = temp;  
  17.     return j;  
  18. }  
  19.   
  20. void quickSort(int start, int end)  
  21. {  
  22.     int mid;  
  23.     if (start < end)  
  24.     {     
  25.         mid = patition(start, end);  
  26.         quickSort(start, mid-1);  
  27.         quickSort(mid+1, end);  
  28.     }  
  29. }  
  30.   
  31. void print(void)  
  32. {  
  33.     printf("%d", seq[0]);  
  34.     for (i = 1; i < n; i++)  
  35.         printf(" %d", seq[i]);  
  36.     puts("");     
  37. }  
  38.   
  39. int main()  
  40. {  
  41.     scanf("%d", &t);  
  42.     while (t--)  
  43.     {  
  44.         scanf("%d", &n);  
  45.         for (i = 0; i < n; i++)  
  46.             scanf("%d", &seq[i]);  
  47.         quickSort(0, n-1);  
  48.         print();  
  49.     }  
  50.     return 0;  
  51. }  


你可能感兴趣的:(J#)