0
0
DSA C++programming~10 mins

Bubble Sort Algorithm in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to swap two elements in the array.

DSA C++
int temp = arr[i];
arr[i] = arr[[1]];
arr[[1]] = temp;
Drag options to blanks, or click blank then click option'
Aj
Bi+1
Ci-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index for swapping, like j or i-1.
Swapping with a fixed index like 0 instead of the next element.
2fill in blank
medium

Complete the code to run the inner loop of bubble sort correctly.

DSA C++
for (int j = 0; j < n - [1] - 1; j++) {
    if (arr[j] > arr[j + 1]) {
        // swap
    }
}
Drag options to blanks, or click blank then click option'
Ai
Bj
Cn
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i in the inner loop limit.
Not reducing the inner loop range, causing unnecessary comparisons.
3fill in blank
hard

Fix the error in the outer loop condition to run bubble sort correctly.

DSA C++
for (int i = 0; i < [1]; i++) {
    // inner loop
}
Drag options to blanks, or click blank then click option'
An-1
Bn
Cn+1
Dn/2
Attempts:
3 left
💡 Hint
Common Mistakes
Using n causes one extra pass.
Using n/2 is incorrect for bubble sort passes.
4fill in blank
hard

Fill both blanks to complete the bubble sort inner loop swap condition and swap code.

DSA C++
if (arr[[1]] > arr[[2]]) {
    int temp = arr[[1]];
    arr[[1]] = arr[[2]];
    arr[[2]] = temp;
}
Drag options to blanks, or click blank then click option'
Aj
Bj+1
Ci
Di+1
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j in the inner loop swap.
Swapping wrong indices causing incorrect sorting.
5fill in blank
hard

Fill all three blanks to complete the full bubble sort function.

DSA C++
void bubbleSort(int arr[], int n) {
    for (int i = 0; i < [1]; i++) {
        for (int j = 0; j < n - i - [2]; j++) {
            if (arr[j] > arr[j + [3]]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}
Drag options to blanks, or click blank then click option'
An-1
B1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop limits causing out of bounds errors.
Incorrect indices in comparison leading to wrong sorting.