You are on page 1of 1

BUBBLE SORT

Bubble sort is the most often used sorting technique. This method compares the
two consecutive elements of the list and if they are not in order, the two elements will
be interchanged immediately, if they are in order the two elements will not be
interchanged. This process continues for (N-1) times for sorting an array of size N
After the first pass the largest element will be in the 'N'th position. In the second
pass the second largest element will be in the 'N-1'th position and soon. Thus in each
pass an element with the largest value in the remaining unsorted elements will be
"Bubble up" and hence the name Bubble sort.
Algorithm BUBBLE_SORT(K,N)
/* K is an array consisting of N elements. This procedure sorts the elements into
ascending order using the bubble sort technique. The variables PASS and LAST
denotes the pass counter and position of the last unsorted element respectively. The
variable I is used to index the array element. The variable EXCH is used to count the
number of exchanges made on any pass. All variables are integers.
Step 1 : [Initialize]
LAST N
Step 2 : [Loop on pass index]
Repeat thru step 5 for PASS=1,2,3,. . .N-1
Step 3 : [Initialize exchange counter for this pass]
EXCH 0
Step 4 : [Perform pair wise comparisons unsorted elements]
Repeat for I=1 to LAST-1
if K[I]>K[I+1]
then
K[I] k[I+1]
EXCH EXCH+1
Step 5 : [Any exchange is made on this pass]
if EXCH=0
then
Return
else
LAST LAST -1
Step 6 : [Finished]
Return

You might also like