0% found this document useful (0 votes)
31 views5 pages

DSA Lab Manual (Bubble Sort)

The document outlines the implementation of the Bubble Sort algorithm in C++ for sorting an array of integers in ascending order. It details the algorithm's properties, time and space complexities, and provides a practical demonstration with sample input and output. The conclusion emphasizes Bubble Sort's simplicity and its inefficiency for large datasets due to its O(n^2) time complexity.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

DSA Lab Manual (Bubble Sort)

The document outlines the implementation of the Bubble Sort algorithm in C++ for sorting an array of integers in ascending order. It details the algorithm's properties, time and space complexities, and provides a practical demonstration with sample input and output. The conclusion emphasizes Bubble Sort's simplicity and its inefficiency for large datasets due to its O(n^2) time complexity.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Manual: Data Structures and

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.

Properties of Bubble Sort:

1. Stable Sort: Maintains the relative order of equal elements.


2. Time Complexity:
o Best Case: O(n) (when the array is already sorted).
o Average Case: O(n^2).
o Worst Case: O(n^2).
3. Space Complexity: O(1) (In-place sorting algorithm).

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;

// Function to implement Bubble Sort


void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
// Flag to detect if the array is already sorted
bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
swap(arr[j], arr[j + 1]);
swapped = true;
}
}

// If no elements were swapped, break the loop


if (!swapped) break;
}
}

// Function to print the array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

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];
}

cout << "Original array: ";


printArray(arr, n);

bubbleSort(arr, n);

cout << "Sorted array: ";


printArray(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:

1. What is the basic principle of the Bubble Sort algorithm?


2. How does the best-case time complexity of Bubble Sort differ from the worst-case?
3. Can Bubble Sort be used for large datasets? Why or why not?
4. How does Bubble Sort compare to other sorting algorithms like Quick Sort or Merge
Sort?
5. What changes can be made to optimize Bubble Sort?

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.

DRY-RUN THE BUBBLE SORT

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):

 Compare 64 & 34 → Swap → [34, 64, 25, 12, 22]

 Compare 64 & 25 → Swap → [34, 25, 64, 12, 22]

 Compare 64 & 12 → Swap → [34, 25, 12, 64, 22]

 Compare 64 & 22 → Swap → [34, 25, 12, 22, 64]


After Pass 1: 34, 25, 12, 22, 64 (swapped = true)

Pass 2 (i=1):

 Compare 34 & 25 → Swap → [25, 34, 12, 22, 64]

 Compare 34 & 12 → Swap → [25, 12, 34, 22, 64]

 Compare 34 & 22 → Swap → [25, 12, 22, 34, 64]


After Pass 2: 25, 12, 22, 34, 64 (swapped = true)

Pass 3 (i=2):

 Compare 25 & 12 → Swap → [12, 25, 22, 34, 64]


 Compare 25 & 22 → Swap → [12, 22, 25, 34, 64]
After Pass 3: 12, 22, 25, 34, 64 (swapped = true)

Pass 4 (i=3):

 Compare 12 & 22 → No Swap → Array remains [12, 22, 25, 34, 64]
No swaps occur → swapped = false → Break outer loop.

Final Sorted Array:


12, 22, 25, 34, 64

The algorithm terminates early in the fourth pass since no swaps occur, indicating the array is sorted.

Mathematical Derivation of Bubble Sort Complexity


Bubble Sort's time complexity depends on the number of comparisons and swaps performed. Let's
analyze it for worst-case, best-case, and average-case scenarios.

1. Worst-Case Time Complexity (O(n²))

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).

 In each pass i, the inner loop runs (n - i - 1) times.

Total Comparisons:
n −2
n(n−1)
∑ ( n−i−1 ) =( n−1 )+(n−2)+ …+1= 2
i=0

This summation simplifies to O(n²).

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

2. Best-Case Time Complexity (O(n))

Scenario: Array is already sorted.


Optimization: The swapped flag terminates the algorithm early.
Derivation:

 Pass 1 (outer loop i=0):

o Inner loop runs (n-1) times (all elements compared).


o No swaps occur → swapped = false → outer loop breaks.

 Total Comparisons: n−1 → O(n).

 Swaps: 0 → O (1).

Total Time Complexity:

O(n)+ O (1) =O(n)


3. Average-Case Time Complexity (O(n²))

Scenario: Randomly ordered array.


Derivation:

 The average number of inversions (pairs of elements out of order) in a random array is n(n−1)/4

 Each inversion requires one swap, leading to n(n−1)/4 swaps.

 Comparisons remain n(n−1)/2.

Total Time Complexity:

O(n (n−1)/2)+ O(n(n−1)/4)=O( n 2)


4. Space Complexity

Bubble Sort is in-place, requiring only a constant amount of extra memory for variables like i, j,
and swapped.

Space Complexity= O (1)

Summary

Case Time Complexity Reason

Worst-Case O(n2) All elements reversed.

Best-Case O(n) Early termination with swapped.

Average- O(n2) Average inversions scale with n2n2.


Case

Space O(1) No extra memory used.

Bubble Sort is simple but inefficient for large datasets due to its quadratic time complexity.

You might also like