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.