Array
Pivot
-
Random selected
Low
-
Next pos for < pivot
Mid
-
Current element
High
-
Next pos for > pivot
Call Stack (Pending Ranges)
Stack empty
Current Range:
-
Click "Step" or "Play" to start the visualization
Pivot
< Pivot
= Pivot
> Pivot
Sorted
Out of Range
▲ low - boundary for < pivot
▼ mid - current scan
▲ high - boundary for > pivot
Python Code - Hybrid Quick Sort (DNF)
def _partition(nums, l, h):
pivot = nums[random.randint(l, h)]
low, mid, high = l, l, h
while mid <= high:
if nums[mid] < pivot:
swap(nums[low], nums[mid])
low += 1; mid += 1
elif nums[mid] == pivot:
mid += 1
else: # nums[mid] > pivot
swap(nums[mid], nums[high])
high -= 1
return low, high
def _quickSort(nums, l, h):
if not l < h: return
low, high = _partition(nums, l, h)
_quickSort(nums, l, low - 1)
_quickSort(nums, high + 1, h)