Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to choose the pivot element as the last element of the array.
DSA Javascript
function quickSort(arr) {
if (arr.length <= 1) return arr;
const pivot = arr[1];
// rest of the code
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing the first element as pivot instead of the last.
Using arr.length instead of arr.length - 1.
✗ Incorrect
The pivot is chosen as the last element, which is at index arr.length - 1.
2fill in blank
mediumComplete the code to partition the array by comparing elements with the pivot.
DSA Javascript
function partition(arr, low, high) {
const pivot = arr[high];
let i = low - 1;
for (let j = low; j < high; j++) {
if (arr[j] [1] pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
return i + 1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than operator which reverses the partition logic.
Using >= or <= which includes equality and may cause incorrect swaps.
✗ Incorrect
Elements less than the pivot are moved to the left side during partition.
3fill in blank
hardFix the error in the recursive call to quickSort to sort the left partition.
DSA Javascript
function quickSort(arr, low = 0, high = arr.length - 1) { if (low < high) { const pi = partition(arr, low, high); quickSort(arr, low, [1]); quickSort(arr, pi + 1, high); } return arr; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pi instead of pi - 1 causes infinite recursion.
Using high or low incorrectly changes the partition boundaries.
✗ Incorrect
The left partition ends at pi - 1, so we sort from low to pi - 1.
4fill in blank
hardFill both blanks to complete the recursive calls for sorting left and right partitions.
DSA Javascript
function quickSort(arr, low = 0, high = arr.length - 1) { if (low < high) { const pi = partition(arr, low, high); quickSort(arr, [1], pi - 1); quickSort(arr, pi + 1, [2]); } return arr; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pi instead of low or high causes wrong partition ranges.
Using 0 instead of low or high causes incorrect sorting range.
✗ Incorrect
Left partition is from low to pi - 1, right partition is from pi + 1 to high.
5fill in blank
hardFill all three blanks to complete the quickSort function with partitioning and recursive calls.
DSA Javascript
function quickSort(arr, [1] = 0, [2] = arr.length - 1) { if ([1] < [2]) { const pi = partition(arr, [1], [2]); quickSort(arr, [1], pi - 1); quickSort(arr, pi + 1, [2]); } return arr; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pivot or index as parameter names causes confusion.
Mixing parameter names in recursive calls causes errors.
✗ Incorrect
The function uses low and high as parameters and compares low < high.