出差回来--快速排序

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

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

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

 

你可能感兴趣的:(快速排序)