Summary of sorting algorithms

Source: Internet
Author: User

1. Sorting Algorithms

Exchange class sort- bubble sort cocktail sort odd even sort comb sort Midget sort quick sort smelly cobbler sort Bogo sort

Select Class Sort- Select sort heap sort smooth sort Cartesian tree sort tournament sort ring sort

Insert class Sort- insert sort Hill sort Two fork find Tree sort library sort patiently sort

Merge class sort- merge sort Cascade merge sort Oscillation Merge sort multi-phase merge sort strand sort

Distribution class sort-US flag sort bead sort bucket sort explosion sort count sort pigeon nest Sort adjacent graph sort Cardinal sort Lightning sort interpolation sort

Concurrency class sort-double sequencer batcher Merge network 22 sort network

Mixed class sort-Tim sort introspection sort spread sort reverse sort J sort

Sort by other class-topological sort of pancake sort spaghetti sort

2. Complex analysis

So the sort records for n are larger. The general choice is the sorting method of the time Complexity O (nlog2n).

3. In terms of time complexity

(1) Order of Square (O (n2)): Various simple sorts: direct insertion, direct selection and bubble sorting ;

(2) Linear logarithmic order (O (NLOG2N)) Ordering: fast sorting, heap sorting, and merge sorting ;

(3) O (n1+§)) ordering, § is a constant between 0 and 1: Hill Sort

(4) Order of Linear Order (O (n)): Cardinal Sort, in addition to bucket, box sort.

Description

When the original table is ordered or basically ordered, the direct insertion of sort and bubble sorting will greatly reduce the number of comparisons and move records, time complexity can be reduced to O (n);

While the quick sort is the opposite, when the original table is basically ordered, it will degenerate to bubble sort, and the time complexity is increased to O (N2);

The order of the original table, the simple selection of sorting, heap sorting, merge sorting and the time complexity of the base sort has little effect.

4. Stability

The stability of the sorting algorithm: if there are multiple records with the same keyword in the sequence to be sorted, the relative order of the records remains unchanged, the algorithm is said to be stable, and if the relative order of the records is changed after sorting, the algorithm is said to be unstable.

Stability Benefits: If the sorting algorithm is stable, sort from one key, then sort from another key, the result of the first key sort can be used for ordering the second key. The Cardinal sort is like this, first by the low sort, successively by the high order, the low-level same element its sequence is also the same time will not change. In addition, if the sorting algorithm is stable, redundant comparisons can be avoided;

stable sorting algorithms : bubble sort, insert sort, merge sort, and Cardinal sort

not a stable sorting algorithm : Select sort, quick sort, hill sort, heap sort

5. Select the sorting algorithm guidelines

Each sorting algorithm has its advantages and disadvantages. Therefore, when practical, it is necessary to choose according to different circumstances, or even combine various methods to use.

Select the basis of the sorting algorithm: there are many factors affecting the sequencing, the algorithm with low average time complexity is not necessarily optimal. Conversely, sometimes an algorithm with a high average time complexity may be better suited to some particular situation. At the same time, the selection algorithm should also consider its readability, in order to facilitate the maintenance of software. In general, there are four things to consider:

(1) The size of the number of records to be sorted n;

(2) The size of the data volume of the record itself, that is, the size of other information except the keywords in the record;

(3) The structure and distribution of the keywords;

(4) Requirements for sequencing stability.

6. Algorithm usage Policy

Set the number of elements to be sorted n.

(1) When n is larger, it should be sorted by the time complexity O (nlog2n): Quick sort, heap sort, or merge sort order.

Quick Sort : is currently considered the best method based on the comparison of internal sorting, when the keywords to be sorted are randomly distributed, the average time of the fast sorting is shortest;

Heap Sort : If memory space permits and requires stability,

Merge Sort : It has a certain number of data movement, so we may have a combination of the insertion sort, first get a certain length of sequence, and then merge, the efficiency will be improved.

(2) When n is large, memory space is allowed, and stability is required using merge sort

(3) When n is small, direct insertion or direct selection can be used.

Direct Insert sort : When the elements are distributed in an orderly manner, inserting the sort directly will significantly reduce the number of comparisons and moves recorded.

Direct Selection sort : element distribution is ordered, if stability is not required, select Direct Select sort

(4) Generally do not use or not directly use the traditional bubble sort.

(5) Radix sorting it is a stable sorting algorithm, but it has some limitations:

[1] keywords can be decomposed.

[2] The number of key bits recorded is less, if dense is better

[3] If it is a number, it is best to be unsigned, otherwise it will increase the corresponding mapping complexity, you can first and the positive and negative sorting.

Summary of sorting algorithms

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.