Heapify – Python 3 (Week 16 – 01)

class Solution:
    """
    @param: A: Given an integer array
    @return: nothing
    """
    
    def siftup(self, A, now):
        while now:
            father = (now - 1) // 2
            #if right son: ((i*2+2) - 1) // 2 = i
            #if left son:((i*2+1) - 1) // 2 = i
            # 
            if A[now] > A[father]:
                break
            A[now], A[father] = A[father], A[now]
            
            now = father
            
            
    def heapify(self, A):
        # write your code here
        for i in range(len(A)):
            self.siftup(A, i)

Leave a comment

Design a site like this with WordPress.com
Get started