0

Bubble Sort

In bubble sort there is a comparison between two adjacent elements and then if the values are not in order  then swapping takes place. The process takes place until no swapping is needed. This is called bubble sort because as the bubble pops upto top in water the smaller elements also pops up to top.

For example:

Lets take an array:

A[4,2,9,3] sort in ascending.

Step 1: [4,2,9,3]- here the first two elements are compared and swapped. New array [2,4,9,3].

Step 2: [2,4,9,3] the next pair is not  swapped as they are in order.

Step3: [2,4,9,3] the last two elements are swapped. New array [2,4,3,6].

Now we have to do one more iteration as the array is still not sorted.

Step 1:[2,4,3,6].

Step 2:[2,3,4,6]

Step 3:[2,3,4,6].

Now the array is sorted and no need to have more iteration.

ks007

Leave a Reply

Your email address will not be published. Required fields are marked *