I'm having trouble understanding why this heap_maker method in python is O(N), I would assume it is O(N log N) simply because we have a nested loop, and each element may be "heaped" down comparing the current loop element to each element in the worst case scenario correct?
def heap_maker(self, ca: ComplexArray) -> None:
"""
Takes a complex array object in as an argument and builds
a heap object based upon the values in the complex array
"""
self._heap = ca #complex array can be considered like a normal python list
#build the heap using heapify down
n = self._heap.length() #length is O(N)
for i in range(n // 2, -1, -1):
j = i
while 2 * j + 1 < n:
left = 2 * j + 1
right = 2 * j + 2 if 2 * j + 2 < n else left
k = left if self._heap[left] < self._heap[right] else right
if self._heap[j] > self._heap[k]:
self._heap[j], self._heap[k] = self._heap[k], self._heap[j]
else:
break
j = k