数据结构

来源:互联网 发布:淘宝联盟怎么样提现 编辑:程序博客网 时间:2024/04/28 09:33

http://student.zjzk.cn/course_ware/data_structure/web/paixu/paixu8.3.2.2.htm

These are all comparison sorts, and so cannot perform better than O(n log n) in the average or worst case.

Comparison sortsNameBestAverageWorstMemoryStableMethodOther notesQuicksortn \log nn \log nn^2\log n on average, worst case is n; Sedgewick variation is \log n worst casetypical in-place sort is not stable; stable versions existPartitioningQuicksort is usually done in place withO(log n) stack space.[citation needed] Most implementations are unstable, as stable in-place partitioning is more complex. Naïvevariants use an O(n) space array to store the partition.[citation needed] Quicksort variant using three-way (fat) partitioning takes O(n) comparisons when sorting an array of equal keys.Merge sortn \log nn \log nn \log nn worst caseYesMergingHighly parallelizable (up to O(log n) using the Three Hungarian's Algorithm[clarification needed] or, more practically, Cole's parallel merge sort) for processing large amounts of data.In-place merge sort——n \log^2 n1YesMergingCan be implemented as a stable sort based on stable in-place merging.[2]Heapsortn \log nn \log nn \log n1NoSelection Insertion sortnn^2n^21YesInsertionO(n + d),[clarification needed] where d is the number of inversions.Introsortn \log nn \log nn \log n\log nNoPartitioning & SelectionUsed in several STL implementations.Selection sortn^2n^2n^21NoSelectionStable with O(n) extra space, for example using lists.[3]Timsortnn \log nn \log nnYesInsertion & MergingMakes n comparisons when the data is already sorted or reverse sorted.Shell sortnn \log^2 n
or
n^{3/2}
Depends on gap sequence;
best known is n \log^2 n
1NoInsertionSmall code size, no use of call stack, reasonably fast, useful where memory is at a premium such as embedded and older mainframe applications.Bubble sortnn^2n^21YesExchangingTiny code size.Binary tree sortnn \log nn \log n (balanced)nYesInsertionWhen using a self-balancing binary search tree.Cycle sort—n^2n^21NoInsertionIn-place with theoretically optimal number of writes.Library sort—n \log nn^2nYesInsertion Patience sorting——n \log nnNoInsertion & SelectionFinds all the longest increasing subsequences in O(n log n).Smoothsortnn \log nn \log n1NoSelectionAn adaptive sort: n comparisons when the data is already sorted, and 0 swaps.Strand sortnn^2n^2nYesSelection Tournament sort—n \log nn \log nn[4] ?Selection Cocktail sortnn^2n^21YesExchanging Comb sortnn \log nn^21NoExchangingSmall code size.Gnome sortnn^2n^21YesExchangingTiny code size.UnShuffle Sort[5]kNkNkNIn place for linked lists. N*sizeof(link) for array.Can be made stable by appending the input order to the key.Distribution and MergeNo exchanges are performed. Performance is independent of data size. The constant 'k' is proportional to the entropy in the input. K = 1 for ordered or ordered by reversed input so runtime is equivalent to checking the order O(N).Franceschini's method[6]n \log nn \log n1Yes? Block sortnn \log nn \log n1YesInsertion & MergingCombine a block-based O(n) in-place merge algorithm[7] with a bottom-up merge sort.

The following table describes integer sorting algorithms and other sorting algorithms that are not comparison sorts. As such, they are not limited by a \Omega(n \log n) lower bound. Complexities below assume n items to be sorted, with keys of size k, digit size d, and r the range of numbers to be sorted. Many of them are based on the assumption that the key size is large enough that all entries have unique key values, and hence that n << 2k, where << means "much less than."

Non-comparison sortsNameBestAverageWorstMemoryStablen << 2kNotesPigeonhole sort—n + 2^kn + 2^k2^kYesYes Bucket sort (uniform keys)—n+kn^2 \cdot kn \cdot kYesNoAssumes uniform distribution of elements from the domain in the array.[8]Bucket sort (integer keys)—n+rn+rn+rYesYesIf r is O(n), then Average is O(n).[9]Counting sort—n+rn+rn+rYesYesIf r is O(n), then Average is O(n).[8]LSD Radix Sort—n \cdot \frac{k}{d}n \cdot \frac{k}{d}nYesNo[8][9]MSD Radix Sort—n \cdot \frac{k}{d}n \cdot \frac{k}{d}n + \frac{k}{d} \cdot 2^dYesNoStable version uses an external array of size n to hold all of the bins.MSD Radix Sort (in-place)—n \cdot \frac{k}{d}n \cdot \frac{k}{d}\frac{k}{d} \cdot 2^dNoNo\frac{k}{d} recursion levels, 2d for count array.Spreadsort—n \cdot \frac{k}{d}n \cdot \left( {\frac{k}{s} + d} \right)\frac{k}{d} \cdot 2^dNoNoAsymptotics are based on the assumption that n << 2k, but the algorithm does not require this.

The following table describes some sorting algorithms that are impractical for real-life use due to extremely poor performance or specialized hardware requirements.

NameBestAverageWorstMemoryStableComparisonOther notesBead sort—N/AN/A—N/ANoRequires specialized hardware.Simple pancake sort—nn\log nNoYesCount is number of flips.Spaghetti (Poll) sortnnnn^2YesPollingThis a linear-time, analog algorithm for sorting a sequence of items, requiring O(n) stack space, and the sort is stable. This requires n parallel processors. See Spaghetti sort#Analysis.Sorting networks\log n\log n\log nn \log nVaries (stable sorting networks require more comparisons)YesOrder of comparisons are set in advance based on a fixed network size. Impractical for more than 32 items.

Theoretical computer scientists have detailed other sorting algorithms that provide better than O(n log n) time complexity assuming additional constraints, including:

  • Han's algorithm, a deterministic algorithm for sorting keys from a domain of finite size, taking O(n log log n) time and O(n) space.[10]
  • Thorup's algorithm, a randomized algorithm for sorting keys from a domain of finite size, taking O(n log log n) time and O(n) space.[11]
  • A randomized integer sorting algorithm taking O\bigl(n \sqrt{\log \log n}\bigr) expected time and O(n) space.[12]

参考

对数函数  http://baike.baidu.com/view/331649.htm

算法时间复杂度计算 http://univasity.iteye.com/blog/1164707

各种算法介绍  http://en.wikipedia.org/wiki/Sorting_algorithm

快速排序java实现  http://www.vogella.com/tutorials/JavaAlgorithmsQuicksort/article.html

0 0
原创粉丝点击