Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the outer loop for selection sort.
DSA Java
for (int i = 0; i [1] arr.length - 1; i++) { // selection sort outer loop }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes index out of bounds error.
Using > or >= reverses the loop logic.
✗ Incorrect
The outer loop runs from 0 to length-2, so the condition is i < arr.length - 1.
2fill in blank
mediumComplete the code to initialize the index of the minimum element inside the outer loop.
DSA Java
int minIndex = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting minIndex to 0 ignores progress of sorting.
Using -1 causes invalid index errors.
✗ Incorrect
We start assuming the minimum element is at the current outer loop index i.
3fill in blank
hardFix the error in the inner loop condition to find the minimum element.
DSA Java
for (int j = i + 1; j [1] arr.length; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes index out of bounds error.
Using > or >= reverses the loop logic.
✗ Incorrect
The inner loop must run while j is less than arr.length to avoid index errors.
4fill in blank
hardFill both blanks to swap the elements at i and minIndex.
DSA Java
int temp = arr[1]; arr[2] = arr[minIndex]; arr[minIndex] = temp;
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets causes syntax errors.
Swapping wrong indices causes incorrect sorting.
✗ Incorrect
Array elements are accessed with square brackets []. We swap arr[i] and arr[minIndex].
5fill in blank
hardFill all three blanks to complete the selection sort method.
DSA Java
public static void selectionSort(int[] arr) {
for (int i = 0; i [1] arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j [2] arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[3];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loops causes index out of bounds.
Using parentheses instead of square brackets for array access.
✗ Incorrect
The outer loop uses '<', inner loop uses '<', and temp stores arr[i] using square brackets.
