Challenge - 5 Problems
Quick Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of Quick Sort Partition Step
What is the output list after the partition step using the last element as pivot in the quick sort algorithm on the list [8, 3, 7, 6, 2]?
DSA Python
def partition(arr, low, high): pivot = arr[high] i = low - 1 for j in range(low, high): if arr[j] <= pivot: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[i+1], arr[high] = arr[high], arr[i+1] return arr arr = [8, 3, 7, 6, 2] result = partition(arr, 0, len(arr)-1) print(result)
Attempts:
2 left
š” Hint
Remember the pivot is the last element and elements smaller or equal go to the left.
ā Incorrect
The partition function moves all elements smaller or equal to the pivot (2) to the left and places the pivot after them. So 2 moves to the front, and 8 moves to the end.
ā Predict Output
intermediateFinal Sorted List by Quick Sort
What is the final sorted list after applying quick sort on [4, 1, 9, 7, 3]?
DSA Python
def quick_sort(arr, low, high): if low < high: pi = partition(arr, low, high) quick_sort(arr, low, pi - 1) quick_sort(arr, pi + 1, high) def partition(arr, low, high): pivot = arr[high] i = low - 1 for j in range(low, high): if arr[j] <= pivot: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[i+1], arr[high] = arr[high], arr[i+1] return i + 1 arr = [4, 1, 9, 7, 3] quick_sort(arr, 0, len(arr)-1) print(arr)
Attempts:
2 left
š” Hint
Quick sort sorts the list in ascending order.
ā Incorrect
Quick sort rearranges the list so that all elements are in ascending order: 1, 3, 4, 7, 9.
š§ Conceptual
advancedUnderstanding Quick Sort Pivot Choice
Which of the following statements about the pivot choice in quick sort is TRUE?
Attempts:
2 left
š” Hint
Think about how pivot choice affects performance on sorted or nearly sorted lists.
ā Incorrect
Choosing a random pivot helps avoid worst-case O(n²) time on already sorted or nearly sorted lists by reducing the chance of unbalanced partitions.
š§ Debug
advancedIdentify the Bug in Quick Sort Partition
What error will this quick sort partition code cause when run on [5, 2, 9, 1]?
DSA Python
def partition(arr, low, high): pivot = arr[low] i = low + 1 for j in range(low + 1, high): if arr[j] < pivot: arr[i], arr[j] = arr[j], arr[i] i += 1 arr[low], arr[i-1] = arr[i-1], arr[low] return i-1 arr = [5, 2, 9, 1] pi = partition(arr, 0, len(arr)) print(arr, pi)
Attempts:
2 left
š” Hint
Check the range used in the for loop and the list length.
ā Incorrect
The for loop uses range(low + 1, high), but high is len(arr), so it excludes the last element index, causing incorrect partition and potential index errors.
š Application
expertQuick Sort Recursive Call Count
Given the list [10, 7, 8, 9, 1, 5], how many total recursive calls to quick_sort (including the initial call) are made until the list is fully sorted?
DSA Python
def quick_sort(arr, low, high, count=[0]): if low < high: count[0] += 1 pi = partition(arr, low, high) quick_sort(arr, low, pi - 1, count) quick_sort(arr, pi + 1, high, count) return count[0] def partition(arr, low, high): pivot = arr[high] i = low - 1 for j in range(low, high): if arr[j] <= pivot: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[i+1], arr[high] = arr[high], arr[i+1] return i + 1 arr = [10, 7, 8, 9, 1, 5] calls = quick_sort(arr, 0, len(arr)-1) print(calls)
Attempts:
2 left
š” Hint
Count each time quick_sort is called with low < high.
ā Incorrect
The quick_sort function is called 9 times in total including the initial call for this list before it is fully sorted.
