Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two integers using pointers.
DSA C
void swap(int *a, int *b) {
int temp = *a;
*a = [1];
*b = temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning *a = temp instead of *a = *b
Using a or b instead of *a or *b
✗ Incorrect
To swap values, assign *a = *b and *b = temp.
2fill in blank
mediumComplete the code to choose the last element as pivot in partition function.
DSA C
int partition(int arr[], int low, int high) {
int pivot = arr[[1]];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing pivot as arr[low] instead of arr[high]
Using an index outside the array bounds
✗ Incorrect
The pivot is chosen as the last element, which is at index high.
3fill in blank
hardFix the error in the recursive quickSort call to sort the left partition.
DSA C
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, [1]);
quickSort(arr, pi + 1, high);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pi + 1 for left partition causing overlap
Using high instead of pi - 1
✗ Incorrect
The left partition ends at pi - 1, so quickSort should be called with that.
4fill in blank
hardFill both blanks to complete the partition function's for loop condition and swap call.
DSA C
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j [1] high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[[2]]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop causing pivot to be included
Swapping with arr[i] instead of arr[j]
✗ Incorrect
The loop runs while j < high, and swap uses arr[j] to swap with arr[i].
5fill in blank
hardFill all three blanks to complete the quickSort function with correct recursive calls and base condition.
DSA C
void quickSort(int arr[], int low, int high) {
if (low [1] high) {
int pi = partition(arr, low, high);
quickSort(arr, low, [2]);
quickSort(arr, [3], high);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in base condition causing infinite recursion
Swapping recursive call bounds
✗ Incorrect
The base condition is low < high; recursive calls are on low to pi-1 and pi+1 to high.