Algorithm-Summary of internal sorting methods

Source: Internet
Author: User

Performance comparison of various sorting methods
Sorting methods The best time complexity Average Time complexity Worst-case complexity of time Complexity of space Stability
Direct Insert Sort O (N) O (N2) O (N2) O (1) Stability
Simple selection sorting O (N2) O (N2) O (N2) O (1) Not stable
Bubble sort O (N) O (N2) O (N2) O (1) Stability
Hill sort O (n1.25) O (1) Not stable
Quick Sort O (NLOG2N) O (NLOG2N) O (N2) O (log2n) ~o (n) Not stable
Heap Sort O (NLOG2N) O (NLOG2N) O (NLOG2N) O (1) Not stable
Merge sort O (NLOG2N) O (NLOG2N) O (NLOG2N) O (1) Stability

Different sorting methods have advantages and disadvantages, can be applied to different occasions according to needs. The main factors to be considered when selecting the sorting method are: The number of records to be sorted n, the size of the record itself, the distribution of the keywords, the requirements for the ordering stability, the conditions of the language tools, the size of the secondary space, and so on.

Based on these factors, the following conclusions can be obtained:

(1) If the number of records to be sorted n is small, insert sort and simple selection sort can be used. Since the record move operation required for the direct insert sort is more than the choice of sorting, it is better to use the direct selection sorting method when the information of the record itself is large.

(2) If the records to be sorted by the basic order of keywords, it is advisable to use direct insertion sort or bubble sort.

(3) When n is large and the number of keywords is small, the chain-based cardinality is better.

(4) If n is large, then the time complexity of O (nlog<sub>2</sub>n) should be used for sorting methods, such as fast sorting, heap sorting or merge sorting.

Fast sorting is considered the best method in the current internal sorting method, and the average time for fast sorting is the shortest when the keywords to be sorted are randomly distributed; heap ordering requires only one secondary storage space and does not appear in the worst case scenario that can occur in a quick sort. Both of these methods are unstable sorting methods, if the order is stable, you can choose merge sort. Merge sort and direct insert sort can often be used together. The long ordered sub-file is obtained by using the direct insert sort, and then 22 is merged.

The internal sorting algorithm is generally implemented on a one-dimensional array. When the information of the record itself is large, the chain list can be used as the storage structure in order to avoid the time-consuming movement records.

Algorithm-Summary of internal sorting methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.