public static void quicksort(int[] array,int low,int high)
{
if(low > high)
{
return;
}
int i=low;
int j=high;
int temp = array[low];//temp就是基准位
while(i<j)
{
while(temp <= array[j] && i<j)//先看右边,依次往左递减
{
j--;
}
while(temp >= array[i] && i<j)//再看左边,依次往右递加
{
i++;
}
if(i < j )//满足条件则交换
{
int t = array[i];
array[i] = array[j];
array[j] = t;
}
}
//最后将基准位与i和j相等位置的数字交换
array[low] = array[i];
array[i] = temp;
//递归调用左半数组
quicksort(array,low,j-1);
//递归调用右半数组
quicksort(array,j+1,high);
}
public static void main(String[] args) {
int[] array = {49, 38, 65, 97, 76, 13, 27, 50 };
System.out.println("排序前的数组:"+Arrays.toString(array));
quicksort(array,0,array.length-1);
System.out.println();
System.out.println("排序后的数组:"+Arrays.toString(array));
}