Bubble sort


Pseudocode

      procedure bubbleSort(A : list of sortable items )
          n = length(A)
          repeat
              swapped = false
              for i = 1 to n-1 inclusive do
                  /* if this pair is out of order */
                  if A[i-1] > A[i] then
                      /* swap them and remember something changed */
                      swap( A[i-1], A[i] )
                      swapped = true
                  end if
              end for
          until not swapped
      end procedure
      

Use this interactive animation to understand it better...

Go ahead, enter a list of numbers separated by commas (eg : 9,8,7,6,5,4,3,2,1 ) and click sort..

( Note : Sorting here in ascending order )




Bubble sort is very simple search algorithm, but surely not very time efficient for large lists, as it's worst time complexity is O( N2 ).