zoukankan      html  css  js  c++  java
  • 出差回来--快速排序

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

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

    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
  • 相关阅读:
    long类型和int类型的区别!!!!!
    Java-冒号的作用
    Java Scanner类
    jre安装过程
    76. 最小覆盖子串
    75. 颜色分类
    73. 矩阵置零
    72. 编辑距离
    71. 简化路径
    61. 旋转链表
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4457960.html
Copyright © 2011-2022 走看看