Bubble Sort Algorithm in DSA Go - Time & Space Complexity
We want to understand how the time taken by Bubble Sort changes as the list size grows.
How many steps does Bubble Sort need to sort a list of numbers?
Analyze the time complexity of the following code snippet.
func bubbleSort(arr []int) {
n := len(arr)
for i := 0; i < n-1; i++ {
for j := 0; j < n-i-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
}
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: 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.
| 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, showing a square growth pattern.
Time Complexity: O(n²)
This means the time to sort grows roughly with the square of the number of items in the list.
[X] Wrong: "Bubble Sort always takes the same time no matter the list order."
[OK] Correct: If the list is already sorted, Bubble Sort can finish faster because it does fewer swaps and can stop early if optimized.
Understanding Bubble Sort's time complexity helps you compare it with faster sorting methods and shows your grasp of algorithm efficiency.
"What if we add a flag to stop early when no swaps happen in a pass? How would the time complexity change?"
Practice
Bubble Sort algorithm?Solution
Step 1: Understand Bubble Sort mechanism
Bubble Sort compares adjacent elements and swaps them if they are out of order.Step 2: Compare with other sorting methods
Other options describe different algorithms: divide and conquer, selection sort, insertion sort.Final Answer:
Repeatedly swap adjacent elements if they are in the wrong order -> Option AQuick Check:
Bubble Sort = swapping adjacent pairs [OK]
- Confusing Bubble Sort with Selection Sort
- Thinking it divides the list like Merge Sort
- Assuming it inserts elements like Insertion Sort
arr of length n?Solution
Step 1: Determine loop range for passes
Bubble Sort requires n-1 passes to ensure sorting, so loop runs from 0 to n-2 (i < n-1).Step 2: Check options for correct syntax and range
for i := 0; i < n-1; i++ {} correctly usesi := 0; i < n-1; i++. Others either run too many or incorrect iterations.Final Answer:
for i := 0; i < n-1; i++ {} -> Option AQuick Check:
Outer loop runs n-1 times [OK]
- Using i <= n causing out-of-range errors
- Starting loop at 1 instead of 0
- Looping n times instead of n-1
arr := []int{5, 3, 8, 4, 2}
n := len(arr)
for j := 0; j < n-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
fmt.Println(arr)Solution
Step 1: Perform one pass of Bubble Sort
Compare and swap adjacent elements: - Compare 5 & 3: swap -> [3 5 8 4 2] - Compare 5 & 8: no swap - Compare 8 & 4: swap -> [3 5 4 8 2] - Compare 8 & 2: swap -> [3 5 4 2 8]Step 2: Final array after one pass
After one pass, largest element 8 moves to the end. Array is [3 5 4 2 8].Final Answer:
[3 5 4 2 8] -> Option CQuick Check:
One pass moves largest to end [OK]
- Assuming full sort after one pass
- Not swapping correctly
- Confusing indices in swaps
arr := []int{4, 2, 7, 1}
n := len(arr)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
fmt.Println(arr)Solution
Step 1: Analyze inner loop range
Inner loop runs j from 0 to n-1, but accesses arr[j+1]. When j = n-1, arr[j+1] is out of range causing runtime error.Step 2: Correct inner loop range
Inner loop should run from 0 to n-2 (j < n-1) to avoid out-of-range access.Final Answer:
Inner loop runs out of range causing runtime error -> Option BQuick Check:
Index out of range error = inner loop limit [OK]
- Not adjusting inner loop limit
- Swapping with wrong comparison
- Misunderstanding loop boundaries
arr := []int{3, 1, 4, 5, 2}, how many total swaps will Bubble Sort perform to fully sort the array in ascending order?Solution
Step 1: Trace swaps in each pass
Initial array: [3,1,4,5,2] Pass 1: - 3 > 1 swap -> [1,3,4,5,2] (1 swap) - 3 > 4 no swap - 4 > 5 no swap - 5 > 2 swap -> [1,3,4,2,5] (2 swaps total) Pass 2: - 1 > 3 no swap - 3 > 4 no swap - 4 > 2 swap -> [1,3,2,4,5] (3 swaps total) Pass 3: - 1 > 3 no swap - 3 > 2 swap -> [1,2,3,4,5] (4 swaps total) Pass 4: - 1 > 2 no swap - 2 > 3 no swap No swaps, sorting done.Step 2: Count total swaps
Pass 1: 2 swaps Pass 2: 1 swap Pass 3: 1 swap Total = 4 swaps Recheck carefully: Pass 1: swaps at (3,1) and (5,2) = 2 Pass 2: swap at (4,2) = 1 Pass 3: swap at (3,2) = 1 Total swaps = 4Step 3: Verify if more swaps needed
Sorting completes after pass 4 with no swaps. Total swaps = 4Final Answer:
4 -> Option DQuick Check:
Count swaps carefully per pass [OK]
- Counting comparisons instead of swaps
- Missing swaps in later passes
- Assuming swaps equal to array length
Ready to prove it?
Pass the test and this pattern moves unknown
