quick sort

quick sort

  1 #include <stdio.h>
  2 
  3  int partition( int a[], int low, int high)
  4 {
  5          int pivotkey = a[low];
  6         a[0] = a[low];
  7          while( low < high)
  8         {
  9                  while(low < high && a[high] >= pivotkey)
 10                         --high;
 11                 a[low] = a[high];
 12                  while(low < high && a[low] <= pivotkey)
 13                         ++low;
 14                 a[high] = a[low];
 15         }
 16         a[low] = a[0];
 17          return low;
 18 }
 19 
 20  void qsort( int a[], int low, int high)
 21 {
 22          int pivotkey;
 23          if(low < high){
 24                 pivotkey = partition(a,low,high);
 25                 qsort(a,low,pivotkey - 1);
 26                 qsort(a,pivotkey + 1,high);
 27         }
 28 }
 29 
 30  void main()
 31 {
 32          int i;
 33          int a[10] = {0,6,5,4,23,54,12,56,87,10};
 34         qsort(a,1,9);
 35          for(i = 1;i<=9;i++)
 36                 printf("%3d",a[i]);
 37         printf("\n");
 38 }

你可能感兴趣的:(quick sort)