Skip to content

Files

Latest commit

bbe578f · Apr 27, 2017

History

History
This branch is up to date with Algorithm-archive/Learn-Data_Structure-Algorithm-by-Javascript:master.

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Apr 27, 2017
Apr 25, 2017
Apr 8, 2017

README.md

Bubble Sort

Bubble Sort is an algorithm which is used to sort N elements that are given in a memory for eg: an Array with N number of elements. Bubble Sort compares all the element one by one and sort them based on their values.

It is called Bubble sort, because with each iteration the smaller element in the list bubbles up towards the first place, just like a water bubble rises up to the water surface.

Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent data items and swapping each pair that is out of order. bubble sort demonstration


A visualization on the algorithm:

Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in the right order (the latter one is smaller than the former one). After each iteration, one less element (the last one) is needed to be compared until there are no more elements left to be compared.

Bubble sort gif

Complexity Analysis

  • Worst Case - O(n2)
  • Average Case - O(n2)
  • Best Case - O(n)

More on this topic