码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2018-02-21 17:10:08      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:end   color   index   快速排序   tin   int   tar   pac   --   

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+",");
        }
    }
}

 

快速排序

标签:end   color   index   快速排序   tin   int   tar   pac   --   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!