Sorting algorithms and their big O’s


Worst Case

Best Case

Average Case

Bubble Sort

O(n2)

O(n)

O(n2)

Insertion Sort

O(n2)

O(n)

O(n2)

Selection Sort

O(n²)

O(n²)

O(n²)

Merge Sort

O(nlogn)

O(nlogn)

O(nlogn)

Quicksort

O(n2)

O(nlogn)

O(nlogn)



Selection Sort:

From Wikipedia:

The algorithm works as follows:

  1. Find the minimum value in the list
  2. Swap it with the value in the first position
  3. Repeat the steps above for the remainder of the list (starting at the second position and advancing each time)

Effectively, the list is divided into two parts: the sublist of items already sorted, which is built up from left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the remainder of the array.