Sorting Algorithm Overview (1)

Source: Internet
Author: User

There are many common sorting algorithms. This article reviews the sorting algorithms from different dimensions before summarizing different sorting algorithms:

[Time complexity]

That is, the total time consumed by sorting. For a sequence list (N in length), the performance is as follows: 1. the best performance is O (n); 2. the average performance is O (n * log n); 3. the worst performance is O (n ^ 2)

The operation on logarithm has the following rule:

1. A ^ (log (a) (B) = B; 2. Log (A) (Mn) = Log (A) (m) + Log () (n); 3. Log (A) (M branch n) = Log (A) (m)-log (A) (n); 4. Log () (M ^ n) = nlog (A) (m); 5. Log (a ^ N) (m) = 1/nlog (A) (m );

[Space complexity]

It mainly refers to the memory size occupied by the Sorting Algorithm Implementation (or the storage space occupied by disk write operations ). Sometimes you can sacrifice some storage space in exchange for better time efficiency.

[Basic idea of sorting]

There are several ways to implement the sorting function: insert, exchange, choice, and merge.

[Stability]

Generally, the algorithm stability means that when two identical records p and q exist in an unordered sequence (P is after Q ). If an algorithm is stable, the obtained sorting result is still after P is in front of Q, while the unstable sorting algorithm may be in front of P after Q.

Unordered sequence (P :( 3,1) q :( 3,7 )) (4, 1) (3, 1) (3, 7) (5, 6)
Stable sorting(The order of p and q has not changed) (3, 1) (3, 7) (4, 1) (5, 6)
Unstable sorting(The order of p and q has changed) (3, 7) (3, 1) (4, 1) (5, 6)

 

 

 

Stable Sorting Algorithm (n is the number of elements in the sequence, and K is the number of different key values)

Sorting Algorithm Maximum time complexity
Bubble sort (also called simple sorting) O (N ^ 2)
Cocktail sorting (cocktail sort, bidirectional bubble sorting) O (N ^ 2)
Insertion Sort (also called simple sorting) O (N ^ 2)
Gnome sorting O (N ^ 2)
Sort by in-situ merge O (N ^ 2)
Bucket sorting) O (n); requires O (k) Additional space
Counting sort) O (N + k); requires O (N + k) Additional space
Merge sort (merge sort) O (nlog N); requires O (n) additional space
Binary Tree sort) O (nlog N) expected time; O (N ^ 2) Worst time; requires O (n) additional space
Pigeonhole sort) O (N + k); requires O (k) extra space
Radix sort) O (n · K); requires O (n) extra space
Library sorting O (nlog n) with high probability, requires (1 + ε) n additional space

 

 

 

 

 

 

 

 

 

 

 

 

Unstable Sorting Algorithm (n is the number of elements in the sequence, and K is the number of different key values)

Sorting Algorithm Maximum time complexity
Selection sort) O (N ^ 2)
Shell sort) O (nlog N)
Combined sorting O (nlog N)
Heapsort) O (nlog N)
Smooth sorting O (nlog N)
Introsort O (nlog N)
Quick Sort) O (nlog N) expected time, O (N ^ 2) Worst case; for large, messy list, it is generally believed to be the fastest known sorting
Patience sorting O (nlog N + k) Worst Case time, requires extra O (N + k) space, and also needs to find the longest incremental sub-serial (longest increasing subsequence)

 

 

 

 

 

 

 

 

Comparison of common algorithms:

Sorting Method Time Complexity Space complexity Stability Complexity
Average Worst Best
Insert sort O (N ^ 2) O (N ^ 2) O (N) O (1) Stability Simple
Hill sorting O (N ^ 1.3)     O (1) Unstable Complicated
Bubble Sorting O (N ^ 2) O (N ^ 2) O (N) O (1) Stability Simple
Quick sorting O (nlog2 (n )) O (N ^ 2) O (nlog2 (n )) O (log2 (n )) Unstable Complicated
Select sort O (N ^ 2) O (N ^ 2) O (N ^ 2) O (1) Unstable Simple
Heap sorting O (nlog2 (n )) O (nlog2 (n )) O (nlog2 (n )) O (1) Unstable Complicated
Merge Sorting O (nlog2 (n )) O (nlog2 (n )) O (nlog2 (n )) O (N) Stability Complicated
Base sort O (D (n + r )) O (D (n + r )) O (D (n + r )) O (r) Stability Complicated

 

 

 

 

 

 

 

 

 

 

 

 

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.