The bubble sort, also known as “Sinking Sort” algorithm is one of the simplest algorithms when it comes to sorting data.


Let’s assume that we have an array “A” that contains “N” number of elements.
The indexes of the elements in the array “A”, would be 0 to N-1.

In bubble sort, the two nearest elements of the array are compared, if the value is out of order, in this case, the smallest value comes after the bigger one, then the swapping of the two elements occurs. This action keeps reaping until no swapping is needed, which indicates that the array or the list is sorted out.

Because of the biggest or the smallest value bubbles up on top of the list, this is called “Bubble Sort”. This algorithm falls into the category of “comparison sort”, where the comparison of elements is used to sort a list.


  • Go
  • Python
  • C

The output of the program above,

Complexity Analysis:

  • Time Complexity

    Best-case: Ω(n)

    Average-case: θ(n²)

    Worst-case: Ο(n²)

  • Space Complexity

    Worst-case: Ο(1) ( which is excellent! )


Due to the simplicity of the algorithm, it is used mostly as an introductory algorithm for people who wants to learn algorithmic computations. Otherwise, the bubble sort algorithm is way too slow to be implemented where speed is of the essence. Sometimes it is used to sort a list which is partially sorted and would occasionally have unsorted data.