0% found this document useful (0 votes)
57 views1 page

CS102 - Lab 3 Manual

The document discusses bubble sort, an algorithm for sorting elements of an array in ascending order. It works by making multiple passes through the array, comparing adjacent elements on each pass and swapping them if they are in descending order. The algorithm pseudocode shows the steps: it repeats a pass from index 1 to N-K for K iterations from 1 to N-1, comparing and potentially swapping adjacent elements each time to push larger values towards the end of the array.

Uploaded by

Atif Rauf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views1 page

CS102 - Lab 3 Manual

The document discusses bubble sort, an algorithm for sorting elements of an array in ascending order. It works by making multiple passes through the array, comparing adjacent elements on each pass and swapping them if they are in descending order. The algorithm pseudocode shows the steps: it repeats a pass from index 1 to N-K for K iterations from 1 to N-1, comparing and potentially swapping adjacent elements each time to push larger values towards the end of the array.

Uploaded by

Atif Rauf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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