• Java QuickSort 快速排序


    Java QuickSort

    /**
     * <html>
     * <body>
     *  <P> Copyright 1994-2018 JasonInternational </p>
     *  <p> All rights reserved.</p>
     *  <p> Created on 2018年4月10日 </p>
     *  <p> Created by Jason</p>
     *  </body>
     * </html>
     */
    package cn.ucaner.algorithm.sorts;
    
    import java.util.Random;
    
    /**
     * Quicksort is a sorting algorithm which, on average, makes O(n*log n) comparisons to sort
     * n items. In the worst case, it makes O(n^2) comparisons, though this behavior is
     * rare. Quicksort is often faster in practice than other algorithms. 
     * <p>
     * Family: Divide and conquer.<br> 
     * Space: In-place.<br>
     * Stable: False.<br>
     * <p>
     * Average case = O(n*log n)<br>
     * Worst case = O(n^2)<br>
     * Best case = O(n) [three-way partition and equal keys]<br>
     * <p>
     * @see <a href="https://en.wikipedia.org/wiki/Quick_sort">Quicksort (Wikipedia)</a>
     * <br>
     * @author Justin Wetherell <phishman3579@gmail.com>
     */
    public class QuickSort<T extends Comparable<T>> {
    
        private static final Random RAND = new Random();
    
        public static enum PIVOT_TYPE {
            FIRST, MIDDLE, RANDOM
        }
    
        public static PIVOT_TYPE type = PIVOT_TYPE.RANDOM;
    
        private QuickSort() { }
    
        public static <T extends Comparable<T>> T[] sort(PIVOT_TYPE pivotType, T[] unsorted) {
            int pivot = 0;
            if (pivotType == PIVOT_TYPE.MIDDLE) {
                pivot = unsorted.length/2;
            } else if (pivotType == PIVOT_TYPE.RANDOM) {
                pivot = getRandom(unsorted.length);  
            }
            sort(pivot, 0, unsorted.length - 1, unsorted);
            return unsorted;
        }
    
        private static <T extends Comparable<T>> void sort(int index, int start, int finish, T[] unsorted) {
            int pivotIndex = start + index;
            T pivot = unsorted[pivotIndex];
            int s = start;
            int f = finish;
            while (s <= f) {
                while (unsorted[s].compareTo(pivot) < 0)
                    s++;
                while (unsorted[f].compareTo(pivot) > 0)
                    f--;
                if (s <= f) {
                    swap(s, f, unsorted);
                    s++;
                    f--;
                }
            }
            if (start < f) {
                pivotIndex = getRandom((f - start) + 1);
                sort(pivotIndex, start, f, unsorted);
            }
            if (s < finish) {
                pivotIndex = getRandom((finish - s) + 1);
                sort(pivotIndex, s, finish, unsorted);
            }
        }
    
        private static final int getRandom(int length) {
            if (type == PIVOT_TYPE.RANDOM && length > 0)
                return RAND.nextInt(length);
            if (type == PIVOT_TYPE.FIRST && length > 0)
                return 0;
            return length / 2;
        }
    
        private static <T extends Comparable<T>> void swap(int index1, int index2, T[] unsorted) {
            T index2Element = unsorted[index1];
            unsorted[index1] = unsorted[index2];
            unsorted[index2] = index2Element;
        }
    }
    

      

  • 相关阅读:
    [笔记]流行数据库SQL差异分析之“不从实体表中取得数据”
    如何评价架构的优劣(转)
    Entity Framework 学习总结之一:ADO.NET 实体框架概述
    [笔记]流行数据库SQL差异分析之“自动增长字段”
    Entity Framework 学习总结之十:加载相关对象
    Entity Framework 学习总结之九:LinqToEntities
    Entity Framework 学习总结之二:数据访问区别
    3、网站前端优化11条笔记汇总
    Entity Framework 学习总结之八:EntitySQL 复杂查询及函数
    Entity Framework 学习总结之七:EntitySQL 基本查询
  • 原文地址:https://www.cnblogs.com/jasonandy/p/9243175.html
Copyright © 2020-2023  润新知