DSA Lab Manual (Bubble Sort)
DSA Lab Manual (Bubble Sort)
Algorithms (DSA)
Experiment Title: Implementation of Bubble Sort Algorithm
Objective:
To understand and implement the Bubble Sort algorithm in C++ for sorting an array of integers
in ascending order.
Theory:
Bubble Sort is a simple sorting algorithm that works by repeatedly stepping through the list to be
sorted, comparing adjacent elements, and swapping them if they are in the wrong order. The pass
through the list is repeated until the list is sorted. The algorithm gets its name because smaller
elements "bubble" to the top of the list.
Algorithm:
1. Start with the first element and compare it with the second element.
2. If the first element is greater than the second, swap them.
3. Move to the next pair of elements and repeat step 2 until the last pair is compared.
4. After each iteration, the largest element is placed in its correct position.
5. Repeat steps 1 to 4 for the remaining unsorted part of the array.
C++ Code:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
bubbleSort(arr, n);
return 0;
}
Practical Demonstration:
1. Input:
o Number of elements: 5
o Elements: 64 34 25 12 22
2. Output:
o Original array: 64 34 25 12 22
o Sorted array: 12 22 25 34 64
Observations:
1. Bubble Sort iterates multiple times over the array to sort it.
2. The number of comparisons decreases with each pass as the largest elements are sorted to
their correct positions.
Viva Questions:
Conclusion:
Bubble Sort is a simple sorting algorithm best suited for small datasets or educational purposes
to understand the basic principles of sorting algorithms. Its performance is not efficient for large
datasets due to its O(n2) time complexity.
The Bubble Sort algorithm is dry-run on the array [64, 34, 25, 12, 22] as follows:
Initial Array:
64, 34, 25, 12, 22
Pass 1 (i=0):
Pass 2 (i=1):
Pass 3 (i=2):
Pass 4 (i=3):
Compare 12 & 22 → No Swap → Array remains [12, 22, 25, 34, 64]
No swaps occur → swapped = false → Break outer loop.
The algorithm terminates early in the fourth pass since no swaps occur, indicating the array is sorted.
Scenario: Array is in reverse order. Every element must be swapped in every pass.
Derivation:
For an array of size n, the algorithm performs (n-1) passes (outer loop i runs from 0 to n-2).
Total Comparisons:
n −2
n(n−1)
∑ ( n−i−1 ) =( n−1 )+(n−2)+ …+1= 2
i=0
Swaps:
n(n−1)
Every comparison leads to a swap, so swaps also equal , giving O(n²).
2
Total Time Complexity:
O ( n ( n−1
2
)
) +O (
2 )
n ( n−1 )
=O ( n )
2
Swaps: 0 → O (1).
The average number of inversions (pairs of elements out of order) in a random array is n(n−1)/4
Bubble Sort is in-place, requiring only a constant amount of extra memory for variables like i, j,
and swapped.
Summary
Bubble Sort is simple but inefficient for large datasets due to its quadratic time complexity.