首页 > 编程语言 > 详细

快速排序

时间:2018-02-21 16:52:21      阅读:253      评论:0      收藏:0      [点我收藏+]
package 算法;

public class QuickSort {
    public static int boundary(int[] arr, int startIndex, int endIndex){
        int standard = arr[startIndex];
        int leftIndex = startIndex;
        int rightIndex = endIndex;
        while(leftIndex < rightIndex){
            while(leftIndex < rightIndex && arr[rightIndex] > standard){
                rightIndex--;
            }
            while(leftIndex < rightIndex && arr[leftIndex] < standard){
                leftIndex++;
            }
            int tmp = arr[rightIndex];
            arr[rightIndex] = arr[leftIndex];
            arr[leftIndex] = tmp;
        }
        arr[rightIndex] = standard;
        return rightIndex;
    }
    public static void quickSort(int[] arr,int startIndex, int endIndex){
        if(startIndex >= endIndex){
            return;
        }
        int boundary = boundary(arr,startIndex,endIndex);
        quickSort(arr,startIndex,boundary-1);
        quickSort(arr,boundary+1,endIndex);
    }
    public static void main(String[] args) {
        //int[] arr = {8,9,7,6,10,2,3,5,4};
        int[] arr = {8,9,7,6};
        quickSort(arr,0,3);
        for(int i : arr){
            System.out.print(i+",");
        }
    }
}

 

快速排序

原文:https://www.cnblogs.com/ysch/p/8456846.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!