Q.... Write a program in any suitable programminglanguage to demonstrate a heap
ID: 3613485 • Letter: Q
Question
Q.... Write a program in any suitable programminglanguage to demonstrate a heap sort. ?
Explanation / Answer
function heapSort(a, count)is input: an unordered arraya of length count (first place a in max-heaporder) heapify(a, count) end := count - 1 while end > 0do (swap theroot(maximum value) of the heap with the last element of theheap) swap(a[end],a[0]) (put the heapback in max-heap order) siftDown(a, 0,end-1) (decrease thesize of the heap by one so that the previous max valuewill stay in itsproper placement) end := end - 1 function heapify(a,count) is (start is assigned the index in a ofthe last parent node) start := (count - 2) / 2 while start 0do (sift down thenode at index start to the proper place such that all nodesbelow the startindex are in heap order) siftDown(a, start,count-1) start := start -1 (after sifting down the root allnodes/elements are in heap order) function siftDown(a, start, end)is input: end representsthe limit of how far down the heap to sift. root := start while root * 2 + 1 enddo (While the root has at least one child) child := root * 2+ 1 (root*2+1 points to the left child) (If the childhas a sibling and the child's value is less than itssibling's...) if child + 1 end anda[child]Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.