MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / sift_down

Method sift_down

data_structures/heap/min_heap.py:67–89  ·  view source on GitHub ↗
(self, idx, array)

Source from the content-addressed store, hash-verified

65
66 # this is min-heapify method
67 def sift_down(self, idx, array):
68 while True:
69 left = self.get_left_child_idx(idx)
70 right = self.get_right_child_idx(idx)
71
72 smallest = idx
73 if left < len(array) and array[left] < array[idx]:
74 smallest = left
75 if right < len(array) and array[right] < array[smallest]:
76 smallest = right
77
78 if smallest != idx:
79 array[idx], array[smallest] = array[smallest], array[idx]
80 (
81 self.idx_of_element[array[idx]],
82 self.idx_of_element[array[smallest]],
83 ) = (
84 self.idx_of_element[array[smallest]],
85 self.idx_of_element[array[idx]],
86 )
87 idx = smallest
88 else:
89 break
90
91 def sift_up(self, idx):
92 p = self.get_parent_idx(idx)

Callers 2

build_heapMethod · 0.95
removeMethod · 0.95

Calls 2

get_left_child_idxMethod · 0.95
get_right_child_idxMethod · 0.95

Tested by

no test coverage detected