Explanation of efficiency of commonly used sorting / finding algorithms

 

 

 

Ranking method Worst time analysis Average time complexity stability Space complexity
Bubble Sort O (n 2 ) O (n2) stable O (1)
Insert sort O (n 2 ) O (n2) stable O (1)
Select sort O (n 2 ) O (n2) stable O (1)
Binary tree sorting O (n 2 ) O(n*log2n) Different O (n)
Quick sort O (n 2 ) O(n*log2n) Unstable O(log2n)~O(n)
Heap sort O(n*log2n) O(n*log2n) Unstable O (1)
Hill Sort THE THE Unstable O (1)

 

Find Average time complexity Search condition Algorithm Description
Sequential search O (n) Unordered or ordered queue Compare each element in order until the keyword is found
Binary search (half search) O (logn) Ordered array The search process starts from the middle element of the array. If the middle element is exactly the element to be searched, the search process ends; if a certain element is greater than or less than the middle element, it searches in the half of the array that is greater or less than the middle element And start with the middle element as before. If the array is empty at a certain step, it means that it cannot be found.
Binary Sort Tree Search O (logn) Binary sort tree In the binary search tree b lookup x procedure as follows:
1. If b is null tree, the search fails
2. If x is equal to b value data field of the root node, then the lookup is successful;
3. If x is less than b of The value of the data field of the root node is searched for the left subtree
4. Find the right subtree.
Hash table method (hash table) O (1) First create a hash table (hash table) According to the key value (Key value) search, through the hash function to locate the data element.
Block search O (logn) Unordered or ordered queue The n data elements are divided into m blocks "in order of blocks" (m ≤ n).
The nodes in each block need not be ordered, but the blocks must be "ordered by block"; that is, the keyword of any element in the first block must be less than the keyword of any element in the second block; and Any element in the second block must be smaller than any element in the third block ... Then use binary search and sequential search.

 

 

Published 22 original articles · Like 3 · Visitor 3442

Guess you like

Origin blog.csdn.net/ChyoD1811/article/details/101348929