def swap(L, i, j): temp = L[i] L[i] = L[j] L[j] = temp def partition(L, first, last): # We pick the element L[first] as the "pivot" around which we partition the list p = first # We process the rest of the elements, one-by-one, in left-to-right order for current in range(p+1, last+1): # If L[current] is smaller than the pivot, it needs to move into the first block, # to the left of the pivot. if L[current] < L[p]: swap(L, current, p+1) swap(L, p, p+1) p = p + 1 return p def generalQuickSort(L, first, last): # Base case: if first == last, then there is only one element in the # slice that needs sorting. So there is nothing to do. # Recursive case: if there are 2 or more elements in the slice L[first:last+1] if first < last: # Divide step: partition returns an index p such that # first <= p <= last and everthing in L[first:p] is <= L[p] # and everything in L[p+1:last+1] is >= L[p] p = partition(L, first, last) # Conquer step generalQuickSort(L, first, p-1) generalQuickSort(L, p+1, last) # Combine step: there is nothing left to do! def quickSort(L): generalQuickSort(L, 0, len(L)-1)