Comparison of various sorting algorithms (2): time complexity and space complexity

Source: Internet
Author: User
Document directory
  • Space complexity
  • The fastest sorting algorithm is bucket sorting.

 

Time Complexity

N ^ 2 indicates the square of N. Selecting sorting is sometimes called directly selecting sorting or simply selecting sorting.

Sorting Method Average time Best time Worst time
Sort buckets (unstable) O (N) O (N) O (N)
Base sorting (stable) O (N) O (N) O (N)
Merge Sorting (stable) O (nlogn) O (nlogn) O (nlogn)
Fast sorting (unstable) O (nlogn) O (nlogn) O (N ^ 2)
Heap sorting (unstable) O (nlogn) O (nlogn) O (nlogn)
Hill sorting (unstable) O (N ^ 1.25)    
Bubble sort (stable) O (N ^ 2) O (N) O (N ^ 2)
Select sorting (unstable) O (N ^ 2) O (N ^ 2) O (N ^ 2)
Insert directly to sort (stable) O (N ^ 2) O (N) O (N ^ 2)

O (n) is an approximation of the time complexity. The order of time 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)

Generally, the time complexity is 2 ^ N (exponential order) and greater time complexity. We will basically not use such an algorithm, which is too impractical. for example, the algorithm for the recursive tower problem is O (2 ^ N ).

Square (N ^ 2) algorithms are barely usable, while nlogn and smaller time complexity algorithms are very efficient algorithms.

 

Space complexity

Bubble sorting, simple selection sorting, heap sorting, and direct insertion sorting. The spatial complexity of hill sorting is O (1), because a temporary variable is required to exchange element positions, (In addition, when traversing a sequence, you must use a variable for indexing)

The complexity of the quick sorting space is logn (because it is called recursively), and the complexity of the Merge Sorting space is O (n). A temporary array with the size of N is required.

The spatial complexity of base sorting is O (n), and the spatial complexity of Bucket sorting is uncertain.

 

 

The fastest sorting algorithm is bucket sorting.

The fastest sorting algorithm is bucket sorting (many people mistakenly think it is fast sorting, but it is not actually. however, in practical applications, quick sorting is often used. However, bucket sorting is usually not used because of several major defects.

1. The elements to be sorted cannot be negative, decimal.

2. The space complexity is uncertain. We need to look at the maximum value of the sorting element.

The size of the auxiliary array 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.