You are on page 1of 1

Data Structures and Algorithms SMIU Course Supervisor: Syeda Nazia Ashraf

LAB No: 3
Objective
Perform Bubble Sort

Theory
Bubble Sort:
The technique we use is called “Bubble Sort” because the bigger value gradually bubbles their
way up to the top of array like air bubble rising in water, while the small values sink to the
bottom of array.
It refers to the operation of rearranging to the elements of Array DATA so that they are in
increasing order, i.e., so that DATA[1]<DATA[2]<DATA[3]<DATA[N]
This technique is to make several passes through the array. On each pass, successive pairs of
elements are compared. If a pair is in increasing order (or the values are identical), we leave the
values as they are. If a pair is in decreasing order, their values are swapped in the array.

Algorithm (Bubble Sort) BUBBLE (DATA, N)


Here DATA is an array with N elements. This algorithm sorts the elements in DATA.

1. Repeat step 2 and 3 for K=1 to N-1


2. Set PTR :=1. [Initialize pass pointer PTR.]
3. Repeat While PTR ≤ N-K: [Executes pass.]
If DATA[PTR] > DATA[PTR+1], then
Interchange DATA[PTR] and DATA[PTR+1]
[End of If structure]
[End of Step 3 inner loop.]
Set PTR:= PTR+1.
[End of Step 1 outer loop.]
4. Exit

You might also like