Sorting and searching

Sorting and searching

Sorting is the act of rearranging a sequence in some known order such as numerical or lexicographical that is either ascending or descending. Sorting arrays are valuable as they enable binary searching which is faster than linear time.

Complexity

Sorting

AlgorithmTimeSpace
Bubble sortO(n^2)O(1)
Insertion sortO(n^2)O(1)
Selection sortO(n^2)O(1)
QuicksortO(n log(n))O(log(n))
MergesortO(n log(n))O(n)
HeapsortO(n log(n))O(1)
Counting sortO(n + k)O(k)
Radix sortO(nk)O(n + k)

Searching

AlgorithmTime
Binary searchO(log(n))

Edge cases

  • Empty sequence
  • Sequence with one element
  • Sequence with two elements
  • Sequence with duplicate elements

References