public class Main
{
public static int partiton(int array[], int low, int high)
{
if(low>high){
return -1;
}
//核心方法,左右扫描,填坑
int key = array[low]; //把第一个设置为基准, 挖坑
//一般不对传入参数直接修改,赋值给临时变量进行修改
int i = low;
int j = high;
while (i < j)
{
while ((array[j] >= key) && (i < j))
{
j--;
}
array[i] = array[j]; //填坑
while ((array[i] <= key) && (i < j))
{
i++;
}
array[j] = array[i]; //填坑
}
array[i] = key; //将基准填坑
return i;
}
public static void quicksort(int array[], int low, int high)
{
//根据基准位置,左右递归调用partition方法
int pivot = 0;
if(low < high)
{
pivot = partiton(array, low, high); //将array数组进行一分为二
quicksort(array, low, pivot - 1); //对低字段表进行递归排序
quicksort(array, pivot + 1, high); //对高字段表进行递归排序
}
}
public static void main(String[] args)
{
// write your code here
int[] a = {34,3,53,2,23,7,14,10};
//数组下标
int low = 0;
int high = a.length-1;
System.out.println("before quicksort:");
for(int i = 0; i < a.length; i++){
System.out.println(a[i]);
}
quicksort(a, low, high);
System.out.println("after quicksort:");
for(int i = 0; i < a.length; i++){
System.out.println(a[i]);
}
}