Challenge - 5 Problems
Bubble Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Bubble Sort on a small array
What is the output of the following C++ code after performing bubble sort on the array?
DSA C++
int arr[] = {5, 3, 8, 4, 2}; int n = 5; 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] << " "; }
Attempts:
2 left
💡 Hint
Bubble sort swaps adjacent elements if they are in the wrong order, pushing the largest to the end each pass.
✗ Incorrect
The bubble sort algorithm compares adjacent elements and swaps them if the left is greater than the right. After all passes, the array is sorted in ascending order.
🧠 Conceptual
intermediate1:00remaining
Number of passes in Bubble Sort
For an array of size n, how many passes does the bubble sort algorithm perform in the worst case?
Attempts:
2 left
💡 Hint
Each pass places the largest unsorted element at its correct position.
✗ Incorrect
Bubble sort performs n-1 passes because after each pass the largest unsorted element is placed at the end, so after n-1 passes the array is sorted.
🔧 Debug
advanced2:00remaining
Identify the error in this Bubble Sort code
What error will this C++ bubble sort code produce when compiled or run?
DSA C++
int arr[] = {4, 2, 7, 1}; int n = 4; for (int i = 0; i < n; 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; } } }
Attempts:
2 left
💡 Hint
Check the inner loop boundary conditions carefully.
✗ Incorrect
The inner loop runs j from 0 to n - i - 1, so arr[j + 1] accesses valid indices. The original code had j < n - i which caused out-of-bounds access.
❓ Predict Output
advanced2:00remaining
Output after optimized Bubble Sort with early stop
What is the output of this C++ code after sorting the array with an early stop optimization?
DSA C++
int arr[] = {1, 2, 3, 4, 5}; int n = 5; bool swapped; for (int i = 0; i < n - 1; i++) { swapped = false; 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; swapped = true; } } if (!swapped) { break; } } for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
The array is already sorted, so the algorithm should stop early.
✗ Incorrect
Since the array is already sorted, no swaps occur and the loop breaks early, leaving the array unchanged.
🧠 Conceptual
expert1:30remaining
Time complexity of Bubble Sort in best and worst cases
What are the time complexities of bubble sort in the best and worst cases respectively?
Attempts:
2 left
💡 Hint
Consider when the array is already sorted versus completely reversed.
✗ Incorrect
In the best case (already sorted), bubble sort only makes one pass with no swaps, so O(n). In the worst case (reverse sorted), it makes n-1 passes with swaps, so O(n^2).