Challenge - 5 Problems
Bubble Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bubble Sort on a small array
What is the output of the following bubble sort code on the array [4, 2, 5, 1]?
DSA C
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; } } } } int main() { int arr[] = {4, 2, 5, 1}; int n = 4; bubbleSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Bubble sort swaps adjacent elements if they are in wrong order, pushing largest to the end each pass.
✗ Incorrect
The bubble sort compares adjacent elements and swaps them if the left is greater than the right. After all passes, the array is sorted ascending: 1 2 4 5.
❓ Predict Output
intermediateNumber of swaps in bubble sort
How many swaps does bubble sort perform on the array [3, 1, 4, 2]?
DSA C
int bubbleSortCountSwaps(int arr[], int n) { int swapCount = 0; 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; swapCount++; } } } return swapCount; } int main() { int arr[] = {3, 1, 4, 2}; int n = 4; int swaps = bubbleSortCountSwaps(arr, n); printf("%d", swaps); return 0; }
Attempts:
2 left
💡 Hint
Count each time two adjacent elements are swapped during sorting.
✗ Incorrect
In the first pass: swaps at positions j=0 (3>1) and j=2 (4>2). In the second pass: swap at j=1 (3>2). Total 3 swaps.
🔧 Debug
advancedIdentify the error in this bubble sort code
What error will this bubble sort code cause when run on array [2, 3, 1]?
DSA C
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; } } } }
Attempts:
2 left
💡 Hint
Check the inner loop boundary and the access to arr[j+1].
✗ Incorrect
The inner loop runs j from 0 to n-i, so arr[j+1] accesses arr[n], which is out of bounds causing runtime error.
🧠 Conceptual
advancedBest case time complexity of bubble sort
What is the best case time complexity of bubble sort when the array is already sorted?
Attempts:
2 left
💡 Hint
Consider if bubble sort can detect the array is sorted early.
✗ Incorrect
With an optimized bubble sort that stops if no swaps occur, best case is O(n) when array is already sorted.
🚀 Application
expertOutput after partial bubble sort passes
Given array [5, 3, 8, 4, 2], what is the array state after exactly two full passes of bubble sort?
DSA C
void bubbleSortPartial(int arr[], int n, int passes) { for (int i = 0; i < passes; 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; } } } } int main() { int arr[] = {5, 3, 8, 4, 2}; int n = 5; bubbleSortPartial(arr, n, 2); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Trace each pass carefully, swapping adjacent elements if needed.
✗ Incorrect
After first pass: [3, 5, 4, 2, 8], after second pass: [3, 4, 2, 5, 8].
