private static void PaiXu(List<int> a, int low, int high)
{
if (low >= high)
{
return;
}
int left = low;
int right = high;
int temp = a[left];//设置基准值
while (left < right)
{
//从右边开始循环,找到比基准值小的,否则high--
while (left < right && a[right] >= temp) right--;
if (left < right)
{
a[left] = a[right];//将比基准值小的数据移位
}
//从左边开始循环,找到比基准值大的,否则low++
while (left < right && a[left] <= temp) left++;
if (left < right)
{
a[right] = a[left];//将比基准值大的数据移位
}
}
//当low==high时,就基准值赋给high/low位置
//此时基准值左边的都比基准值小,右边的都比基准值大
a[left] = temp;
//递归排序基准值左边的序列
PaiXu(a, low, left - 1);
//递归排序基准值右边的序列
PaiXu(a, left + 1, high);
}
public static void main(String[] args) {
int a[] = {3,2,5,8,4,7,6,9};
paixu(a,0,7);
//输出
for(int x=0; x<8; x++){
System.out.print(a[x]+" ");
}
System.out.println();
}