Bubble Sort Method


Bubble sort is a simple sorting method. Bubble sort woks fine for smaller number of elements in the list.
Bubble sort is an example of Internal Sort.
In order to sort N elements using bubble sort technique we required to perform maximum N-1 PASS.
During First PASS 1st and 2nd element are compared and if 1st element is greater then 2nd element then they are interchanged. Now 2nd and 3rd element are compared if 2nd element is greater then 3rd element then they are interchanged. This process is repeated for all remaining elements. Thus after the completion of first PASS the element with the largest value is placed at its proper position.
During Second PASS the same process is repeated and the element with next largest value is placed at its proper position.
During each successive PASS the element with next largest value is placed at its proper position.
During first pass we required N-1 comparisons. During second pass we required N-2 comparisons and during Ith pass we required N-I comparisons.
Thus the order of comparisons is proportional to N2 i.e O(N2)

     

Example of Bubble Sort

Pass1 Pass2 Pass3 Pass4 Pass5 Pass6 Pass7
5
2
2
2
2
-2
-2
-2
2
5
5
3
-2
2
0
0
35
9
3
-2
3
0
2
2
9
3
-2
5
0
3
3
3
3
-2
9
0
5
5
5
5
-2
35
0
9
9
9
9
9
52
0
35
35
35
35
35
35
0
52
52
52
52
52
52
52

Download Projects


Download Programs