Comparison of various sorting algorithms (2): Time complexity, spatial complexity

Source: Internet
Author: User
Complexity of Time

N^2 represents the square of N, and select sort is sometimes called direct select sort or simple select sort

Sorting methods Average Time Best time Worst time
Bucket sequencing (unstable) O (N) O (N) O (N)
Cardinal Sort (Stable) O (N) O (N) O (N)
Merge sort (Stable) O (NLOGN) O (NLOGN) O (NLOGN)
Quick Sort (unstable) O (NLOGN) O (NLOGN) O (n^2)
Heap sort (unstable) O (NLOGN) O (NLOGN) O (NLOGN)
Hill Sort (unstable) O (n^1.25)
Bubble sort (Stable) O (n^2) O (N) O (n^2)
Select Sort (unstable) O (n^2) O (n^2) O (n^2)
Direct Insert sort (Stable) O (n^2) O (N) O (n^2)

O (n) Such a sign is called asymptotic time complexity, which is an approximation. The order of the various asymptotic complexity from small to large is as follows

O (1) < O (Logn) < O (n) < O (Nlogn) < O (n^2) < O (n^3) < O (2^n) < O (n!) < O (n^n)

The general time complexity to 2^n (exponential order) and greater time complexity, such an algorithm we basically do not use, too impractical. For example, the recursive implementation of the Nottingham Tower problem algorithm is O (2^n).

The square order (n^2) algorithm is barely available, while the NLOGN and smaller time complexity algorithms are very efficient algorithms.

Complexity of Space

Bubble sort, simple selection sort, heap sort, direct insert sort, the spatial complexity of the hill sort is O (1), because a temporary variable is needed to swap the element position (and in addition, a variable is used to index the sequence)

The fast sort space complexity is logn (because recursive calls), the merge sort space is O (n), and a temporary array of size n is required.

The spatial complexity of the base sort is O (n), and the spatial complexity of bucket ordering is uncertain

The fastest sort algorithm is the bucket sort

The fastest of all sorting algorithms should be the bucket sort (many people mistakenly think that it is a quick sort, not actually.) But the bucket sort is generally not used much, because there are a few relatively large defects.

1. The element to be sorted cannot be a negative number, a decimal number.

2. Spatial complexity is uncertain, and the maximum value of the ordered element is to be seen.

The required auxiliary array size is the value of the largest element.

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.