首页 > 编程语言 > 详细

出差回来--快速排序

时间:2015-04-26 18:04:06      阅读:240      评论:0      收藏:0      [点我收藏+]

  被导师派出差加上之前的大摩的面试,两周的时间都在飞机或者火车上,弄的回到学校浮躁,状态不是很好。

  调整状态最好的办法就是去做了,今天下午写了一下,一直不愿意写的快速排序算法,分享一下~

技术分享
public class QuickSort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        QuickSort sort = new QuickSort();
        int[] nums = {2,3,3,3,1,2,2,2};
        sort.quickSort(nums, 0, nums.length - 1);
        for(int num: nums)
            System.out.print(num+" ");
    }
    public void quickSort(int[] nums, int left, int right)
    {
        if(left < right)
        {
            int index = partition(nums, left, right);
            quickSort(nums, left, index - 1);
            quickSort(nums, index + 1, right);
        }
    }
    public int partition(int[] nums, int left, int right)
    {
        int key = nums[left];
        int low = left; 
        int high = right;
        while(low < high)
        {
            while(low < high && nums[high] >= key) high--;
            nums[low] = nums[high];
            while(low < high && nums[low] < key ) low++;
            nums[high] = nums[low];
        }
        nums[low] = key;
        return low;
        
    }

}
View Code

 

出差回来--快速排序

原文:http://www.cnblogs.com/sunshisonghit/p/4457960.html

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