Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
In bubble sort, we swap the current element arr[i] with the next element arr[i+1] if they are out of order.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The inner loop runs fewer times each pass because the largest elements bubble to the end. So it runs up to n - i - 1.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n causes one extra pass.
Using n/2 is incorrect for bubble sort passes.
✗ Incorrect
The outer loop runs n-1 times because after n-1 passes, the array is sorted.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j in the inner loop swap.
Swapping wrong indices causing incorrect sorting.
✗ Incorrect
We compare arr[j] with arr[j+1] and swap if arr[j] is greater to bubble the largest element up.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop limits causing out of bounds errors.
Incorrect indices in comparison leading to wrong sorting.
✗ Incorrect
The outer loop runs n-1 times, inner loop runs n - i - 1 times, and we compare arr[j] with arr[j+1].