Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to select the pivot element as the last element of the list.
DSA Python
def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[[1]] left = [x for x in arr[:-1] if x <= pivot] right = [x for x in arr[:-1] if x > pivot] return quick_sort(left) + [pivot] + quick_sort(right)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as pivot index selects the first element, not the last.
Using len(arr) causes an index error because it's out of range.
✗ Incorrect
The pivot is chosen as the last element, which is at index -1 in Python lists.
2fill in blank
mediumComplete the code to partition the list into elements less than or equal to the pivot.
DSA Python
left = [x for x in arr[:-1] if x [1] pivot]
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' puts larger elements on the left, which is incorrect.
Using '==' only selects elements equal to pivot, missing smaller ones.
✗ Incorrect
We want elements less than or equal to the pivot on the left side.
3fill in blank
hardFix the error in the recursive call to quick_sort on the right partition.
DSA Python
return quick_sort(left) + [pivot] + quick_sort([1])
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr[:-1] re-sorts the whole list except last element.
Using arr[1:] excludes the first element but not partitioned correctly.
✗ Incorrect
The right partition is stored in the variable 'right', so we must call quick_sort(right).
4fill in blank
hardFill both blanks to complete the partitioning of the list into left and right parts.
DSA Python
left = [x for x in arr[:-1] if x [1] pivot] right = [x for x in arr[:-1] if x [2] pivot]
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' for left excludes elements equal to pivot.
Using '<=' for right includes elements that should be on left.
✗ Incorrect
Left contains elements less than or equal to pivot; right contains elements greater than pivot.
5fill in blank
hardFill all three blanks to complete the quick sort function with pivot selection, partitioning, and recursive calls.
DSA Python
def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[[1]] left = [x for x in arr[:-1] if x [2] pivot] right = [x for x in arr[:-1] if x [3] pivot] return quick_sort(left) + [pivot] + quick_sort(right)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as pivot index selects first element, not last.
Mixing up comparison operators in partitioning.
✗ Incorrect
Pivot is last element (index -1), left has elements <= pivot, right has elements > pivot.
