Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the array.
DSA Java
int temp = arr[[1]]; arr[[1]] = arr[j]; arr[j] = temp;
Attempts:
3 left
💡 Hint
Common Mistakes
Using the pivot index instead of i or j.
Swapping with the wrong index causing incorrect partition.
✗ Incorrect
We swap the element at index i with the element at index j to rearrange elements around the pivot.
2fill in blank
mediumComplete the code to choose the pivot element for partitioning.
DSA Java
int pivot = arr[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Choosing the first element as pivot without adjusting the algorithm.
Using an index outside the current sub-array.
✗ Incorrect
The pivot is commonly chosen as the last element in the current sub-array, which is at index high.
3fill in blank
hardFix the error in the recursive quickSort call to sort the left partition.
DSA Java
quickSort(arr, low, [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using pivotIndex instead of pivotIndex - 1 causes infinite recursion.
Using pivotIndex + 1 sorts the right partition instead.
✗ Incorrect
The left partition ends at pivotIndex - 1 because pivotIndex is the pivot's final position.
4fill in blank
hardFill both blanks to complete the partition method's loop condition and increment.
DSA Java
for (int j = low; j [1] high; j[2]) { if (arr[j] <= pivot) { i++; // swap arr[i] and arr[j] } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes the loop to include the pivot element.
Using '--' decrements j causing an infinite loop.
✗ Incorrect
The loop runs while j is less than high, and j increments by 1 each iteration.
5fill in blank
hardFill all three blanks to complete the quickSort recursive calls and base case check.
DSA Java
if (low [1] high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, [2]); quickSort(arr, [3], high); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' in base case causes unnecessary recursion.
Swapping pivotIndex - 1 and pivotIndex + 1 in recursive calls causes wrong sorting.
✗ Incorrect
The base case is when low is less than high. Then quickSort is called on left (low to pivotIndex-1) and right (pivotIndex+1 to high) partitions.
