def del_min(self): min = 0 n = len(self.items) for i in range(n): if self.items[i] < self.items[min]: min = i exch(self.items, min, n - 1) return self.pop()
def del_max(self): max = 0 n = len(self.items) for i in range(n): if self.items[i] > self.items[max]: max = i exch(self.items, max, n - 1) return self.pop()
def sort(a: list): n = len(a) for i in range(n): for j in range(i, -1, -1): if a[j] < a[j - 1]: exch(a, j, j - 1) else: break return a
def partition(a, lo, hi): i, j = lo+1, hi mid = a[lo] while True: while a[i] < mid: if i == hi: break i += 1 while a[j] > mid: if j == lo: break j -= 1 if i >= j: break exch(a, i, j) exch(a, lo, j) return j
def percolating_down(self, i): while i * 2 <= self.current_size: min_c = self.min_child(i) if self.heap_list[i] > self.heap_list[min_c]: exch(self.heap_list, i, min_c) i = min_c
def percolation_up(self, i): while i // 2 > 0: if self.heap_list[i] < self.heap_list[i // 2]: exch(self.heap_list, i, i // 2) i //= 2