The body of this method is like this:
def quick_sort(L):
if len(L) >= 2:
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]]))
else:
return L
The reason why we can use recursion to sort a list is because once we have oriented a number, the elements on the left side are always smaller than the oriented number so that we can regard the left side part as a new list and do the same thing again ( because all the numbers in the new list are smaller than the oriented number, when we organize the new list, it won't mess the original list). Similarly, we can use the method to organize the numbers on the right side. Once we finished organizing left side part and right side part, we can get a sorted list.