BubbleSort_Example
BubbleSort_Example
Below, we have a pictorial representation of how bubble sort will sort the given array.
Step/pass 1: when i = 0
a[0] a[1] a[2] a[3] a[4]
j=0 4 6 2 9 1 4 > 6 (False), No Swapping
Step/pass 2: when i = 1
a[0] a[1] a[2] a[3] a[4]
j=0 4 2 6 1 9 4 > 2 (True), Swapping happens
Step/pass 3: when i = 2
a[0] a[1] a[2] a[3] a[4]
j=0 2 4 1 6 9 2 > 4 (False), No Swapping
Step/pass 4: when i = 3
a[0] a[1] a[2] a[3] a[4]
j=0 2 1 4 6 9 2 > 1 (True), Swapping happens
So, as we can see in the representation above, after the first step/pass, 9 is placed at the last
index, which is the correct position for it.
Similarly, after the second step/pass, 6 is placed at the second last index, and so on.