def partition(a, start, end): pivot = a[end] i = start - 1 # ix of the last element in "left", initially outside the list for j in range(start, end): if a[j] <= pivot: i += 1 swap(a, i, j) swap(a, i + 1, end) return i + 1 # position of the pivot, "start" of next iterations
def reverse(array): length = len(array) mid = math.floor(length / 2) # swap elements on either side of the midpoint ix = 0 while ix < mid: swap(array, ix, length - ix - 1) ix += 1 return array
def insert_in_sorted_order(array, el): # replace the empty spot with the element array[-1] = el # swap backwards until element is in the right place cur = len(array) - 1 # index of the element to insert ix = cur - 1 # start a second-to-last while ix >= 0 and array[ix] > array[cur]: swap(array, ix, cur) cur = ix ix -= 1 return array