Now, we have divide the whole array into three parts, left part of the pivot, pivot and the right part of the pivot. We know that at this level, this three part is already sorted. I mean the values in each area is sorted in a macro level. But within each part, the values might not be sorted. For this reason, we notice that the left part and the right part can be treated as a new array and we can use the same method to keep dividing it into smaller parts until we reach the bottom of the array. That is where we can apply recursion.
# simple quick sort
def quick_sort(L):
''' (list) -> list
Return a list containing the same elements as L in ascending order.
>>> quick_sort([])
[]
>>> quick_sort([5])
[5]
>>> import random
>>> L = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(L)
>>> quick_sort(L)
[1, 2, 3, 4, 5, 6, 7]
'''
if len(L) < 2: # there's nothing to sort
return L
else:
return (quick_sort([x for x in L if x < L[0]]) +
[L[0]] +
quick_sort([x for x in L[1:] if x >= L[0]]))
here is the code from professor's lecture note. Note the recursion part
return (quick_sort([x for x in L if x < L[0]]) +
[L[0]] +
quick_sort([x for x in L[1:] if x >= L[0]]))
we keep calling function quick_sort recursively to get the sorted list. But here,
we always choose the first element of the array as the pivot point. If we encounter
a sorted list which means the first element is the smallest one, then we have a
very unbalanced pivot list and this algorithm will be very slow.