Sorting
Heap Sort
The binary heap data structure is a array data structure as a almost full binary tree. Here is a provided way to implement to compute the indices of the parent node, left, and right child
Algorithm 1 Parent
procedure Parent()
return
end procedure
Algorithm 2 Left
procedure Left()
return
end procedure
Algorithm 3 Right
procedure Right()
return
end procedure
There are two ways to implement the binary heaps, the max-heaps and min-heaps. Here we focus on the max-heap such that it obeys the max-heap property as follows
The
running time is logarthimic time, which maintains the max-heap property.The
running time is linear time, builds a max-heap from an unordered input array.The
running time is which sorts the array in place.
Algorithm 4 Max-Heapify
procedure Max-Heapify()
Left
Right
if .heap-size and then
largest
else
largest
end if
if .heap-size and then
end if
if then
exchange with
Max-Heapify()
end if
end procedure
Building a heap
Algorithm 5 Build-Max-Heap
procedure Build-Max-Heap()
heap-size =
for down to 1 do
Max-Heapify()
end for
end procedure