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 steps increase when sorting bigger lists?
Analyze the time complexity of the following code snippet.
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 the inner loop.
- How many times: The inner loop runs roughly n times for each of the n passes of the outer loop, so about 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 list size, so bigger lists take much longer.
[X] Wrong: "Bubble Sort only takes time proportional to the list size, so it is O(n)."
[OK] Correct: Because Bubble Sort compares pairs many times in nested loops, the total steps grow much faster than just 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 add a flag to stop early when no swaps happen? 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 continues until the list is sorted, which matches Repeatedly swap adjacent elements if they are in the wrong order.Final Answer:
Repeatedly swap adjacent elements if they are in the wrong order -> Option DQuick Check:
Bubble Sort = Swap neighbors [OK]
- Confusing Bubble Sort with Quick Sort or Merge Sort
- Thinking it divides the list into parts
- Assuming it inserts elements like Insertion Sort
arr[i] and arr[i+1] in C# during Bubble Sort?Solution
Step 1: Understand swapping in C#
C# does not support tuple unpacking or direct swap functions by default, so a temporary variable is needed.Step 2: Identify correct swap syntax
Using a temporary variable to hold one value, then swapping is correct as in temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp;.Final Answer:
temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; -> Option AQuick Check:
Use temp variable to swap [OK]
- Trying to swap without a temp variable
- Using Python-style tuple swap syntax
- Assuming a built-in swap function exists
int[] arr = {5, 3, 8, 4};
for (int i = 0; i < arr.Length - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
Console.WriteLine(string.Join(", ", arr));Solution
Step 1: Perform one pass of Bubble Sort on array
Start with arr = [5, 3, 8, 4]. Compare pairs:
5 > 3? Yes, swap -> [3, 5, 8, 4]
5 > 8? No swap
8 > 4? Yes, swap -> [3, 5, 4, 8]Step 2: Final array after one pass
After one pass, array is [3, 5, 4, 8].Final Answer:
3, 5, 4, 8 -> Option AQuick Check:
One pass swaps neighbors to bubble largest to end [OK]
- Assuming full sort after one pass
- Not swapping last pair correctly
- Confusing indices in loop
int[] arr = {4, 2, 7, 1};
for (int i = 0; i < arr.Length; i++) {
for (int j = 0; j < arr.Length; 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 range
The inner loop uses j from 0 to arr.Length, but accesses arr[j+1]. This causes out-of-range error when j = arr.Length - 1.Step 2: Fix inner loop condition
Changing inner loop to j < arr.Length - 1 prevents accessing arr[arr.Length], fixing the error.Final Answer:
Change inner loop condition to j < arr.Length - 1 -> Option BQuick Check:
Prevent out-of-range by limiting inner loop [OK]
- Not adjusting inner loop range
- Changing outer loop incorrectly
- Misunderstanding which indices to swap
int[] arr = {7, 2, 9, 4, 3}, how many total swaps will Bubble Sort perform to fully sort the array in ascending order?Solution
Step 1: Trace swaps during Bubble Sort passes
Initial array: [7, 2, 9, 4, 3]
Pass 1:
7 > 2? Swap -> [2, 7, 9, 4, 3] (1 swap)
7 > 9? No swap
9 > 4? Swap -> [2, 7, 4, 9, 3] (2 swaps)
9 > 3? Swap -> [2, 7, 4, 3, 9] (3 swaps)
Pass 2:
2 > 7? No swap
7 > 4? Swap -> [2, 4, 7, 3, 9] (4 swaps)
7 > 3? Swap -> [2, 4, 3, 7, 9] (5 swaps)
Pass 3:
2 > 4? No swap
4 > 3? Swap -> [2, 3, 4, 7, 9] (6 swaps)
Pass 4:
No swaps needed, array sorted.Step 2: Count total swaps
Total swaps = 6.Final Answer:
6 -> Option CQuick Check:
Count swaps each pass carefully [OK]
- Counting only first pass swaps
- Forgetting swaps in later passes
- Assuming swaps equal number of elements
Ready to prove it?
Pass the test and this pattern moves unknown
