Computer Applications

Explain the technique of Bubble Sort with an example.

Java Arrays

13 Likes

Answer

Bubble Sort is a sorting algorithm that works by repeatedly iterating through the array, comparing each pair of adjoining elements and swapping them if they are in wrong order.

For example, consider the following unsorted array:

9523

Pass 1

First 9 is compared with 5 and as 9 is greater than 5 the elements are swapped:

5923

Next, 9 is compared with 2 and as 9 is greater than 2 the elements are swapped:

5293

Next, 9 is compared with 3 and as 9 is greater than 3 the elements are swapped:

5239

At the end of first pass, the highest element of the array is at the last position.

Pass 2

5 is compared with 2 and as 5 is greater than 2 the elements are swapped:

2539

Next, 5 is compared with 3 and as 5 is greater than 3 the elements are swapped:

2359

At the end of first pass, the second highest element of the array is in its correct position.

Pass 3

2 is compared with 3 and as 2 is less then 3 no swapping takes place.

2359

With this, the third and final pass ends and the elements of the array are in sorted order.

Answered By

6 Likes


Related Questions