快速排序

来源:互联网 发布:js跨域请求 编辑:程序博客网 时间:2024/06/07 06:43

快速排序中在确定那个分割的中间量的时候需要注意几个循环的使用。

public static int partition(int []num,int low,int high){
int index=num[low];
 while(low<high){
while(low<high&&num[high]>index)
{high--;}//此处右边大括号不可以放置在下面位置1处
if(low<high)   
num[low]=num[high];//1
while(low<high&&index>num[low])
{low++;}//此处右边大括号不可以放置在下面位置2处
if(low<high)
num[high]=num[low];//2
}
num[low]=index;//重新赋值给num[0];
return low;//返回分割点
}


public static void quicksort(int num[],int low,int high)
{
if(low<high)
int mid=partition(num,low,high);
quicksort(num,low,mid-1);
quicksort(num,mid+1,high);
}

0 0