Commonly used time complexity sorting

time complexity

n^2 means n squared, selection sort is sometimes called direct selection sort or simple selection sort

 

Sort method Average time best time worst time
bucket sort (unstable) O(n) O(n) O(n)
Radix sort (stable) O(n) O(n) O(n)
merge sort (stable) O(nlogn) O(nlogn) O(nlogn)
quicksort (unstable) O(nlogn) O(nlogn) O(n^2)
Heapsort (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)
selection sort (unstable) O(n^2) O(n^2) O(n^2)
Direct Insertion Sort (stable) O(n^2) O(n) O(n^2)

A sign like O(n) is called asymptotic time complexity, which is an approximation. The order of various asymptotic time complexities 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 has reached 2^n (exponential order) and greater time complexity. We basically will not use such an algorithm , it is too impractical. For example, the recursive implementation of the Tower of Hanoi algorithm is O(2^ n).

The algorithm of square order (n^2) is barely usable, and the time complexity algorithm of nlogn and smaller is a very efficient algorithm.

 

space complexity

The space complexity of bubble sort, simple selection sort, heap sort, direct insertion sort, and Hill sort is O(1), because a temporary variable is needed to exchange element positions, (in addition, a variable is inevitably used when traversing the sequence. index)

The space complexity of quicksort is logn (because of the recursive call), and the space complexity of merge sort is O(n), requiring a temporary array of size n.

The space complexity of radix sort is O(n), and the space complexity of bucket sort is uncertain

 

 

The fastest sorting algorithm is bucket sort

The fastest of all sorting algorithms should be bucket sorting (many people mistakenly think it is quick sorting, but it is not. However, in practical applications, quick sorting is often used), but bucket sorting is generally not used much, because there are several large ones. defect.

1. The elements to be sorted cannot be negative numbers or decimals.

2. The space complexity is uncertain, depending on the maximum value of the sorted elements.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326360242&siteId=291194637