Here is a clearer implementation of the section step:
def quicksort(arr, low, high): if low > high or low == high: return pivot = randint(low, high) updated_pivot = partition(pivot,arr, low, high) quicksort(arr, low, updated_pivot-1) quicksort(arr, updated_pivot+1, high) def partition(pivot, arr, low, high): arr[low], arr[pivot] = arr[pivot], arr[low]
EXAMPLE OF WORK:
Input:
[5,1,3,8, 0,2] | pivot
Separation Algorithm:
[3,1,5,8,0,2] --> after switching pivot position | pivot start | [3,1,5,8,0,2] --> initializing start and curr | curr start | [3,1,5,8,0,2] --> 1 < 3, so we swap 1 & 1, and start++, curr++ | curr start | [3,1,5,8,0,2] --> 5 > 3, so we don't swap. Don't move start, curr++ | curr start | [3,1,5,8,0,2] --> 8 > 3, so we don't swap. Don't move start, curr++ | curr start | [3,1,0,8,5,2] --> 0 < 3, so we swap 5 and 0. start++, curr++ | curr start | [3,1,0,2,5,8] --> 2 < 3, so we swap 8 and 2. start++, curr++ | curr [2,1,0,3,5,8] --> swap 2 and 3, and reset pivot index.
Conclusion:
[2,1,0,3,5,8] | pivot