/** * 快速排序 * @author Administrator * */ class Quicksort { private int partition(int sortArray[],int low,int hight) { int key = sortArray[low]; //判定条件必须小于 如果小于等于,跳出的时候low会大于hight //key的置换发生在low == hight中 while(low<hight) { while(low<hight && sortArray[hight]>=key) hight--; sortArray[low] = sortArray[hight]; while(low<hight && sortArray[low]<=key) low++; sortArray[hight] = sortArray[low]; } sortArray[low] = key; return low; } public void sort(int low,int hight, int data[]) { if(low<hight) { int result = partition(data,low,hight); sort(low,result-1, data); sort(result+1,hight,data); } } public void display(int data[]) { for(int i=0;i<data.length;i++) { System.out.print(data[i]); System.out.print(" "); } } public static void main(String[] args) { // TODO 自动生成方法存根 Quicksort qs = new Quicksort(); int data[] = {44,22,2,32,54,22,88,77,99,11}; qs.sort(0, data.length-1, data); qs.display(data); } }