您的位置:首页 > 编程语言 > Java开发

快速排序_Java

2017-05-15 10:02 204 查看
public class Sort005 {
public static int getMiddle(int[] numbers, int low,int high)

    {
int temp = numbers[low]; //数组的第一个作为中轴

        while(low < high){
       while(low < high && numbers[high] > temp)
           high--;
       numbers[low] = numbers[high];//比中轴小的记录移到低端
       while(low < high && numbers[low] < temp)
           low++;
       numbers[high] = numbers[low] ; //比中轴大的记录移到高端

        }

        numbers[low] = temp ; //中轴记录到尾

        return low ; // 返回中轴的位置

    }
public static void quickSort(int[] numbers,int low,int high)

    {

        if(low < high)

        {

        int middle = getMiddle(numbers,low,high); //以numbers[middle]的值将numbers数组进行一分为二

        quickSort(numbers, low, middle-1);   //对低字段表进行递归排序

        quickSort(numbers, middle+1, high); //对高字段表进行递归排序

        }

    

    }
public static void main(String [] args){
int [] a={5,4,9,8,7,6,0,1,3,2};
quickSort(a,0,a.length-1);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 快速排序