0
0
DSA C++programming~20 mins

Bubble Sort Algorithm in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bubble Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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] << " ";
}
A2 3 4 5 8
B5 3 8 4 2
C8 5 4 3 2
D2 4 3 5 8
Attempts:
2 left
💡 Hint
Bubble sort swaps adjacent elements if they are in the wrong order, pushing the largest to the end each pass.
🧠 Conceptual
intermediate
1: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?
An passes
Bn + 1 passes
Cn - 1 passes
Dn / 2 passes
Attempts:
2 left
💡 Hint
Each pass places the largest unsorted element at its correct position.
🔧 Debug
advanced
2: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;
        }
    }
}
ARuntime error: out-of-bounds access
BNo error, sorts correctly
CCompilation error: missing semicolon
DLogic error: array remains unsorted
Attempts:
2 left
💡 Hint
Check the inner loop boundary conditions carefully.
Predict Output
advanced
2: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] << " ";
}
A2 1 3 4 5
B5 4 3 2 1
C1 3 2 4 5
D1 2 3 4 5
Attempts:
2 left
💡 Hint
The array is already sorted, so the algorithm should stop early.
🧠 Conceptual
expert
1: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?
ABest: O(log n), Worst: O(n^2)
BBest: O(n), Worst: O(n^2)
CBest: O(n^2), Worst: O(n^2)
DBest: O(n), Worst: O(n log n)
Attempts:
2 left
💡 Hint
Consider when the array is already sorted versus completely reversed.