So I understand how the partition works, however, I do not understand how the quicksort function works. This is code I found online however, most versions are pretty similar. How does the quicksort function piece together the entire sorted list? I don't understand how the return statement returns a whole sorted list when the partitions are making subsets of the list. So shouldn't the return value here be one or two numbers?
What I'm looking for is an explanation for how the_quicksort()function runs, step by step. Anyone's help is much appreciated!
def partition(xs, start, end):
follower = leader = start
while leader < end:
if xs[leader] <= xs[end]:
xs[follower], xs[leader] = xs[leader], xs[follower]
follower += 1
leader += 1
xs[follower], xs[end] = xs[end], xs[follower]
return follower
def _quicksort(xs, start, end):
if start >= end:
return
p = partition(xs, start, end)
_quicksort(xs, start, p-1)
_quicksort(xs, p+1, end)
def quicksort(xs):
_quicksort(xs, 0, len(xs)-1)