public class QuickSortDemo {
/**
* @param args
*/
public static void main(String[] args) {
int[] array = {5, 3, 6, 2, 7, 1, 9, 4, 8, 11, 4, 17,9};
quickSort(array, 0, array.length - 1);
for (int i = 0; i < array.length; i ++) {
System.out.print(array[i] + " ");
}
}
public static void quickSort(int array[], int low, int high) {
if (low < high) {
int middle = getMiddle(array, low, high);
quickSort(array, low, middle - 1);
quickSort(array, middle + 1, high);
}
}
public static int getMiddle(int[] arry,int start,int end)
{
int pivot=arry[start];//将首元素最为pivot
while(start<end)
{
//找到第一个大于pivot的arry[end]
while(pivot<=arry[end]&&start<end)
end--;
arry[start]=arry[end];
//找到第一个小于pivot的arry[start]
while(pivot>=arry[start]&&start<end)
start++;
arry[end]=arry[start];
}
//最后start==end以后的位置就是起始pivot存放的位置
arry[start]=pivot;
//返回pivot的位置,后面按次位置分解
return start;
}
}