Sabtu, 14 Mei 2011

How to use Bubble Sort


What is Bubble Sort


Bubble sort is one of the sorting algorithms used for arranging the elements in an array. Bubble sort can be used in and Java programming languages. Also bubble sort is known as simple sort as it compares the elements in the array and then interchange the pair of elements if necessary. This step is carried out till the last pair of elements in the array or until the elements in the array got sorted in the specific order. Let's now see an example how bubble sort works

Example


The following elements in the array are to be sorted using bubble sort
[6 2 5 4 3]
Now the bubble sort compares the first pair of elements in the array (6 and 2). 6 greater than 2 and the condition is true. Hence the elements will be swapped as 2 and 6. Now the elements in the array will be ordered as
[2 6 5 4 3]
Bubble sort compares the next pair of elements in the array (6 and 5). 6 greater than 5 and the condition is true. Hence the elements will be swapped as 5 and 6. Now the elements in the array will be ordered as
[2 5 6 4 3]
Next pair of elements is considered (6 and 4). 6 greater than 4 and the condition is true. Hence the elements will be swapped as 4 and 6. Now the elements in the array will be ordered as
[2 5 4 6 3]
Last pair of elements considered now (6 and 3). 6 greater than 3 and the condition is true. Hence the elements will be swapped as 3 and 6. Now the elements in the array will be ordered as
[2 5 4 3 6]
Now the first pass has been completed i.e., the elements have been swapped in the array. But still we didn't get the elements arranged in ascending ordered. So bubble sort will start the second pass. The first pair of elements in the array is scanned now (2 and 5). Already the elements are in the specific order; bubble sort will not swap the elements. The next pair is now considered (5 and 4). 5 greater than 4 and the condition is true. Hence the elements will be swapped as 4 and 5. Now the elements in the array will be ordered as
[2 4 5 3 6]
Next pair of elements is considered (5 and 3). 5 greater than 3 and the condition is true. The elements will be swapped and the array will be ordered as
[2 4 3 5 6]
Last pair of elements 5 and 6 is in specific order. So bubble sort will not swap them and the third pass starts since the elements are not arranged in the specific order. The first pair of elements in the array is scanned now (2 and 4). Already the elements are in the specific order; bubble sort will not swap the elements. The next pair is now considered (4 and 3). 4 greater than 3 and the condition is true. Hence the elements will be swapped as 4 and 5. Now the elements in the array will be ordered as
[2 3 4 5 6]
The remaining pair of elements is in the specific order and now we have achieved the goal of using the bubble sort. Since the elements in the array are arranged in the ascending order, bubble sort will end its process in the third pass.

The basic concept behind this bubble sort is that when comparing the first two elements, the first element will be stored in Temp location and the next element will be assigned to X. The value of Temp will be assigned to Y as mentioned below.

temp = array[X]
array[X] = array[Y]
array[Y] = temp

0 komentar:

Posting Komentar

VISITORS

Flag Counter