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 many steps does it take to sort a list of numbers?
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.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing and possibly swapping adjacent elements inside nested loops.
- How many times: Outer loop runs (n-1) times, inner loop runs decreasing times from (n-1) down to 1.
As the list size grows, the number of comparisons grows roughly with the square of the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 45 comparisons |
| 100 | About 4,950 comparisons |
| 1000 | About 499,500 comparisons |
Pattern observation: Doubling the input size roughly quadruples the work done.
Time Complexity: O(n²)
This means the time to sort grows quickly as the list gets bigger, roughly proportional to the square of the list size.
[X] Wrong: "Bubble Sort only takes time proportional to the list size (O(n)) because it just compares neighbors."
[OK] Correct: Because it uses nested loops, it compares many pairs multiple times, making the total steps 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.
"What if we stop the algorithm early if no swaps happen in a full pass? How would the time complexity change in the best case?"
Practice
Bubble Sort Algorithm?Solution
Step 1: Understand the sorting method
Bubble sort works by comparing adjacent elements and swapping them if they are out of order.Step 2: Identify the correct description
Repeatedly swap adjacent elements if they are in the wrong order describes this swapping of neighbors repeatedly until sorted.Final Answer:
Repeatedly swap adjacent elements if they are in the wrong order -> Option CQuick Check:
Bubble sort = swapping neighbors [OK]
- Confusing bubble sort with divide and conquer methods
- Thinking bubble sort uses a pivot element
- Mixing bubble sort with insertion sort logic
arr of size n?Solution
Step 1: Understand the inner loop range
The inner loop runs from 0 ton - i - 1because after each pass, the lastielements are sorted.Step 2: Match the correct loop syntax
for (int j = 0; j < n - i - 1; j++) correctly usesfor (int j = 0; j < n - i - 1; j++)to avoid out-of-bound access.Final Answer:
for (int j = 0; j < n - i - 1; j++) -> Option BQuick Check:
Inner loop limit = n - i - 1 [OK]
- Using j < n causes out-of-bound access
- Starting j from 1 misses first element comparison
- Using <= instead of < causes extra iteration
int arr[] = {4, 2, 3};
int n = 3;
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++) {
printf("%d ", arr[i]);
}Solution
Step 1: Trace first pass of bubble sort
Compare 4 and 2, swap -> arr = {2, 4, 3}; then compare 4 and 3, swap -> arr = {2, 3, 4}Step 2: Trace second pass and final array
Second pass compares 2 and 3 (no swap), 3 and 4 (no swap). Array remains {2, 3, 4}.Final Answer:
2 3 4 -> Option DQuick Check:
Sorted array = 2 3 4 [OK]
- Not swapping correctly on first pass
- Printing unsorted array
- Confusing loop limits causing partial sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}Solution
Step 1: Analyze loop ranges for bubble sort
The outer loop should befor (int i = 0; i < n - 1; i++)because aftern-1passes, array is sorted, avoiding an unnecessary pass.Step 2: Identify the fix
Change outer loop to for (int i = 0; i < n - 1; i++) correctly optimizes by limiting ton-1passes.Final Answer:
Change outer loop to for (int i = 0; i < n - 1; i++) -> Option AQuick Check:
Outer loop limit = n - 1 [OK]
- Running outer loop n times wastes time
- Changing inner loop start incorrectly
- Removing swap breaks sorting logic
arr = {5, 1, 4, 2, 8}, how many total swaps will bubble sort perform to sort it in ascending order?Solution
Step 1: Trace pass 1 (i=0, j < 4)
Initial: {5,1,4,2,8}
j=0: 5>1 swap -> {1,5,4,2,8} (1)
j=1: 5>4 swap -> {1,4,5,2,8} (2)
j=2: 5>2 swap -> {1,4,2,5,8} (3)
j=3: 5<8 no -> 3 swaps, array: {1,4,2,5,8}Step 2: Trace pass 2 (i=1, j < 3)
j=0: 1<4 no
j=1: 4>2 swap -> {1,2,4,5,8} (4)
j=2: 4<5 no -> 1 swap, total 4, array: {1,2,4,5,8}Step 3: Trace pass 3 (i=2, j < 2)
j=0: 1<2 no
j=1: 2<4 no -> 0 swaps, total 4Step 4: Trace pass 4 (i=3, j < 1)
j=0: 1<2 no -> 0 swaps, total 4Step 5: Total swaps counted = 4
But we missed one swap in pass 2: Actually, pass 2 has 1 swap (4>2), pass 3 has 0 swaps, pass 4 has 0 swaps. Total swaps = 4.Recounting carefully:
Pass 1: 3 swaps
Pass 2: 1 swap
Pass 3: 0 swaps
Pass 4: 0 swaps
Total = 4 swapsFinal Answer:
4 -> Option AQuick Check:
Total swaps counted = 4 [OK]
- Counting non-swaps as swaps
- Assuming max swaps per pass without checking
- Forgetting that last elements get sorted early
Ready to prove it?
Pass the test and this pattern moves unknown
