快排写法

 1      private static void qsort_asc(int source[],int low,int high)
 2      {
 3          if(low<high)
 4          {
 5              int i=low;
 6              int j=high;
 7              int x=source[i];
 8              while(i<j)
 9              {
10                  while(i<j&&x<source[j])
11                      j--;
12                  if(i<j)
13                  {
14                      source[i]=source[j];
15                  }
16                  while(i<j&&x>source[i])
17                      i++;
18                  if(i<j)
19                      source[j]=source[i];
20              }
21              source[i]=x;
22              qsort_asc(source,low,i-1);
23              qsort_asc(source,i+1,high);
24          }
25      }

一个函数搞定

 

 1  private static int partion(int source[],int low,int high)
 2      {
 3          int x=source[low];
 4          if(low<high)
 5          {
 6              while(low<high)
 7              {
 8                  while(low<high&&x<source[high])
 9                      high--;
10                  if(low<high)
11                  {
12                      source[low]=source[high];
13                  }
14                  while(low<high&&x>source[low])
15                      low++;
16                  if(low<high)
17                      source[high]=source[low];
18              }
19              source[low]=x;
20          }
21          return low;
22      }
23      private static void QSort(int source[],int low,int high)
24      {
25          if(low<high)
26          {
27              int pivotpost=partion(source,low,high);
28              QSort(source,low,pivotpost-1);
29              QSort(source,pivotpost+1,high);
30          }
31      }

 

你可能感兴趣的:(写)