Chapter 4: QuickSort
def quicksort(array): if len(array) < 2: return array //Base case: arrays with 0 or 1 element are already “sorted.” else: pivot = array[0] // Recursive case less = [i for i in array[1:] if i <= pivot] //Sub-array (Less than pivot) greater = [i for i in array[1:] if i > pivot] //Sub-array (Greater than pivot) return quicksort(less) + [pivot] + quicksort(greater) print quicksort([10, 5, 2, 3]) =>[2,3,5,10]
Last updated