Sorting Algorithms

Concept Quizzes

Challenge Quizzes

Heap Sort

A crucial element of the Heap_Sort is the construction of the heap itself. This is achieved by calling a heapify procedure on some nodes until all nodes satisfy the Max_Heap property( Any node must have a value less or equal to that of its parent ).

The node in position 222 violates the heap property. Once heapify is applied to it, which position will it be in?

defHeap_Sort(A):Build_Heap(A)foriinrange(A.size,1,-1):#loop down from A.size to 2# !MISSING LINE !!!A.size-=1Max_Heapify(A,1)returnlist(A.A)

Consider the Heap_Sort method above. Given a list of numbers A it will construct a heap and sort them. One line is however missing(line 4). What line of code should be there in order to have a properly functioning Heap_Sort algorithm.

A crucial element of the Heap_Sort is the construction of the heap itself. This is achieved by calling a heapify procedure on some nodes until all nodes satisfy the Max_Heap property( Any node must have a value less or equal to that of its parent ).

Suppose we have a an array with 515151 elements. At most, how many calls to heapify are required to convert it to a heap?