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

快速排序

时间:2016-02-29 19:43:16      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

public class QuickSort {
    public static int getMidIndex(int[] list, int low, int high) {
        int temp = list[low];
        while(low < high) {
            while(low<high && list[high] >= temp) {
                high --;
            }
            list[low] = list[high];
            while(low<high && list[low] <= temp) {
                low ++;
            }
            list[high] = list[low];
        }
        list[low] = temp;
        return low;
    }
    
    public static void quickSort(int[] list, int low, int high) {
        //递归一定要注意递归结束的条件,否则就是死循环
        if(low < high) {
            int middle = getMidIndex(list, low, high);
            quickSort(list, low, middle-1);
            quickSort(list, middle+1, high);
        }
        
    }
    
    public static void main(String[] args) {
        int[] list = { 8, 23,6, 6,7,7,3,4,0,9, 17};
        quickSort(list, 0, list.length-1);
        for(int i = 0 ; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
        
        
    }

}

 

快速排序

标签:

原文地址:http://www.cnblogs.com/leehfly/p/5228633.html

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