How bubble sort works and provide example?

Answer:
Bubble sort work by comparing values of two numbers and then swapping them if they are in the wrong order until no more swapping can be done.

procedure bubbleSort( A : list of sortable items ) defined as:
do
swapped := false
for each i in 0 to length(A) - 1 inclusive do:
if A[i] > A[i+1] then
swap( A[i], A[i+1] )
swapped := true
end if
end for
while swapped
end procedure

First answer by Vafloyd. Last edit by Vafloyd. Contributor trust: 79 [recommend contributor recommended]. Question popularity: 44 [recommend question].