1)快速排序的基本思想:
设当前待排序的无序区为R[low......high],在R[low......high]中任选一个记录作为基准(Pivot),以此基准将当前无序区划分为左、右两个较小的子区间R[low..pivotpos-1)和R[pivotpos+1..high],并使左边子区间中所有记录的关键字均不大于基准,右边的子区间中所有记录的关键字均不小于基准。基准记录不参与下次排序,它已经处于排好位pivotpos。下次递归调用快速排序对左、右子区间R[low..pivotpos-1]和R[pivotpos+1..high]快速排序。
2)具体代码:
[code]
package com.amplesky;
public class QuickSort
{
public static void main(String[] args)
{
int [] a = {5,6,7,65,3,4,2,6,4,5};
int low = 0;
int heigh = a.length -1;
quickSort(a,low,heigh);
for (int i =0; i <= heigh ;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void quickSort(int a[], int low,int heigh)
{
if( low >= heigh)
{
return ;
}
int index = partion(a, low, heigh);
quickSort(a,low, index -1);
quickSort(a,index+1, heigh);
}
public static int partion(int a[], int low,int heigh)
{
int swap = a[low];
while(low < heigh)
{
while(low < heigh && a[heigh] >= swap)
{
heigh -- ;
}
if(low < heigh)
{
a[low++] = a[heigh];
}
while(low < heigh && a[low] <= swap)
{
low ++ ;
}
if(low < heigh)
{
a[heigh--] = a[low];
}
}
a[low]= swap;
return low;
}
}
[/code]