Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the array.
DSA C
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning '*a' to '*b' instead of using the temporary variable.
Using the wrong variable name in the swap.
✗ Incorrect
The variable 'temp' temporarily holds the value of '*a' so we can assign '*b' to '*a' and then assign 'temp' to '*b' to complete the swap.
2fill in blank
mediumComplete the code to find the index of the minimum element in the unsorted part of the array.
DSA C
int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] [1] arr[minIndex]) { minIndex = j; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' which finds the maximum instead of minimum.
Using '==' which does not update the minimum index.
✗ Incorrect
We compare if arr[j] is less than arr[minIndex] to find the smallest element's index.
3fill in blank
hardFix the error in the loop condition to correctly iterate over the array.
DSA C
for (int i = 0; i [1] n - 1; i++) { // selection sort steps }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes the loop to run one extra time and may cause errors.
Using '>' or '>=' causes the loop to never run.
✗ Incorrect
The loop should run while i is less than n - 1 to avoid going out of bounds.
4fill in blank
hardFill both blanks to complete the selection sort inner loop and swap step.
DSA C
for (int j = i + 1; j [1] n; j++) { if (arr[j] [2] arr[minIndex]) { minIndex = j; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' in the loop condition causes out of bounds errors.
Using '>' in the comparison finds the maximum instead of minimum.
✗ Incorrect
The inner loop runs while j is less than n, and we check if arr[j] is less than arr[minIndex] to find the minimum.
5fill in blank
hardFill all three blanks to complete the selection sort function.
DSA C
void selectionSort(int arr[], int n) {
for (int i = 0; i [1] n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j [2] n; j++) {
if (arr[j] [3] arr[minIndex]) {
minIndex = j;
}
}
swap(&arr[minIndex], &arr[i]);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' in outer loop causes extra iteration.
Using '>' in comparison finds maximum instead of minimum.
✗ Incorrect
The outer loop runs while i < n-1, the inner loop runs while j < n, and the comparison uses '<' to find the minimum element.
