Bubble Sort Algorithm in DSA C++ - Time & Space Complexity
We want to understand how the time taken by Bubble Sort changes as the list size grows.
How does the number of comparisons and swaps increase when the list gets bigger?
Analyze the time complexity of the following code snippet.
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
This code sorts an array by repeatedly swapping adjacent elements if they are in the wrong order.
- Primary operation: Comparing and possibly swapping adjacent elements inside the inner loop.
- How many times: The inner loop runs about n times for each of the n passes of the outer loop, so roughly n x n = n² times.
As the list size grows, the number of comparisons grows much faster because each element is compared with many others multiple times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 45 comparisons |
| 100 | About 4,950 comparisons |
| 1000 | About 499,500 comparisons |
Pattern observation: When the input size doubles, the operations roughly quadruple, showing a square growth pattern.
Time Complexity: O(n²)
This means the time taken grows roughly with the square of the number of elements, so sorting gets much slower as the list grows.
[X] Wrong: "Bubble Sort only takes time proportional to the number of elements (O(n)) because it just compares neighbors."
[OK] Correct: Each element is compared many times in nested loops, so the total comparisons grow much faster than the list size.
Understanding Bubble Sort's time complexity helps you appreciate why more efficient sorting methods are used in real projects and shows your grasp of algorithm basics.
"What if we add a flag to stop early if no swaps happen in a pass? How would the time complexity change in the best case?"
Practice
Bubble Sort algorithm?Solution
Step 1: Understand Bubble Sort process
Bubble Sort compares adjacent elements and swaps them if they are out of order.Step 2: Identify the correct description
This swapping of neighbors repeats until the whole list is sorted.Final Answer:
Repeatedly swap adjacent elements if they are in the wrong order -> Option BQuick Check:
Bubble Sort = Swap neighbors repeatedly [OK]
- Confusing Bubble Sort with Selection Sort
- Thinking it divides the array like Merge Sort
- Assuming it inserts elements like Insertion Sort
arr of size n?Solution
Step 1: Understand inner loop range
The inner loop runs from 0 ton - i - 1because after each pass, the lastielements are sorted.Step 2: Check each option
for (int j = 0; j < n - i - 1; j++) correctly usesj < n - i - 1to avoid out-of-bound and unnecessary comparisons.Final Answer:
for (int j = 0; j < n - i - 1; j++) -> Option AQuick Check:
Inner loop limit = n - i - 1 [OK]
- Using j < n instead of j < n - i - 1
- Starting j from 1 instead of 0
- Using <= instead of < causing out-of-bound
int arr[] = {5, 3, 8, 4};
int n = 4;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}Solution
Step 1: Trace first pass of Bubble Sort
Compare pairs: (5,3) swap -> {3,5,8,4}, (5,8) no swap, (8,4) swap -> {3,5,4,8}Step 2: Trace second pass and final array
Next pass: (3,5) no swap, (5,4) swap -> {3,4,5,8}, last pass no swaps needed.Final Answer:
3 4 5 8 -> Option DQuick Check:
Sorted array = 3 4 5 8 [OK]
- Not swapping correctly on first pass
- Confusing order of swaps
- Printing unsorted array
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}Solution
Step 1: Analyze inner loop condition
Inner loop uses j < n - i, but accesses arr[j + 1], which causes out-of-bound when j = n - i - 1.Step 2: Correct inner loop limit
Changing to j < n - i - 1 prevents accessing arr[n], fixing runtime error.Final Answer:
Change inner loop to j < n - i - 1 to avoid out-of-bound access -> Option CQuick Check:
Inner loop limit fixes out-of-bound [OK]
- Ignoring out-of-bound access
- Changing outer loop only
- Assuming code is correct without checking array access
arr = {7, 2, 9, 4, 3}, how many total swaps will Bubble Sort perform to sort it in ascending order?Solution
Step 1: Trace swaps in each pass
Pass 1: (7,2) swap, (7,9) no, (9,4) swap, (9,3) swap -> 3 swaps
Array now: {2,7,4,3,9}Step 2: Continue tracing swaps
Pass 2: (2,7) no, (7,4) swap, (7,3) swap -> 2 swaps
Array now: {2,4,3,7,9}
Pass 3: (2,4) no, (4,3) swap -> 1 swap
Array now: {2,3,4,7,9}
Pass 4: no swapsFinal Answer:
6 -> Option AQuick Check:
Total swaps counted = 6 [OK]
- Counting comparisons instead of swaps
- Stopping early without counting all passes
- Miscounting swaps in passes
Ready to prove it?
Pass the test and this pattern moves unknown
