Time and space complexity of common sorting and search algorithms

Time and space complexity of commonly used sorting algorithms

 

Sorting

worst time analysis

Average time complexity

stability

space complexity

Bubble Sort

O(n2)

O(n2)

Stablize

O(1)

Insertion sort

O(n2)

O(n2)

Stablize

O(1)

selection sort

O(n2)

O(n2)

Stablize

O(1)

Binary tree sorting

O(n2)

O(n*log2n)

different

O(n)

quicksort

O(n2)

O(n*log2n)

unstable

O(log2n)~O(n)

heap sort

O(n*log2n)

O(n*log2n)

unstable

O(1)

Hill sort

O(n2)

O (nlogn) -O (n2)

unstable

O(1)

Find Algorithm Time Complexity

find

Average time complexity

Find conditions

Algorithm Description

Sequential search

O(n)

unordered or ordered queue

Compare each element in order until the key is found

Binary search (half search)

O (logn)

sorted array

The search process starts from the middle element of the array. If the middle element is exactly the element to be found, the search process ends; if a specific 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 the comparison from the middle element just like the beginning. If the array is empty at a certain step, it means it was not found.

Binary Sort Tree Search

O (logn)

binary sorted tree

The process of finding x in a binary search tree b is:

1.  If b is an empty tree, the search fails

2.  If x is equal to the value of the data field of the root node of b, the search is successful;

3.  If x is less than the value of the data field of the root node of b, search the left subtree

4.  Find the right subtree.

Hash table method (hash table)

O(1)

First create a hash table (hash table)

Search according to the key value method (Key value) , and locate the data element through the hash function.

block lookup

O (logn)

unordered or ordered queue

Divide n data elements "in block order" into m blocks (m ≤ n).

The nodes in each block do not have to be ordered, but the blocks must be " ordered by block"; that is, the key of any element in the first block must be smaller than the key of any element in the second block; and Any element in block 2 must be less than any element in block 3, . . . Then use binary search and sequential search.

 

 

Guess you like

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