Docsity

Heaps and Heap Sort: Data Structures and Time Complexity Analysis, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Heaps and Heap Sort: Data Structures and Time Complexity Analysis and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Heap and Heap Sort

Course: Data Structure and Algorithms

Heap

A heap is a data structure that stores a

collection of objects (with keys), and has the

following properties:

• Complete Binary tree

• Heap Order

It is implemented as an array where each node

in the tree corresponds to an element of the

array.

Heap

• The root of the tree A[1] and given index i of

a node, the indices of its parent, left child

and right child can be computed

PARENT ( i)

return floor( i/2)

LEFT ( i)

return 2 i

RIGHT ( i)

return 2 i + 1

Heap order property

• For every node v, other than the root, the key

stored in v is greater or equal (smaller or equal

for max heap) than the key stored in the

parent of v.

• In this case the maximum value is stored in

the root

Max Heap Example

Array A

Min heap example

Array A

Insert 17

swap

Percolate up to maintain the

heap property

Deletion

• Delete max

• Copy the last number to the root ( overwrite the maximum

element stored there ).

• Restore the max heap property by percolate down.

• Delete min

• Copy the last number to the root ( overwrite the minimum

element stored there ).

• Restore the min heap property by percolate down.

Procedures on Heap

• Heapify

• Build Heap

• Heap Sort

Heapify

  • (^) Heapify picks the largest child key and compare it to

the parent key. If parent key is larger than heapify

quits, otherwise it swaps the parent key with the

largest child key. So that the parent is now becomes

larger than its children.

Heapify(A, i)

lleft(i)

rright(i)

if l <= heapsize[A] and A[l] > A[i]

then largestl

else largesti

if r <= heapsize[A] and A[r] > A[largest]

then largestr

if largest != i

then swap A[i]  A[largest]

Heapify(A, largest)

Heap Sort Algorithm

• The heap sort algorithm starts by using procedure

BUILD-HEAP to build a heap on the input array A[..

n]. Since the maximum element of the array stored at

the root A[1], it can be put into its correct final

position by exchanging it with A[ n] (the last element

in A). If we now discard node n from the heap than the

remaining elements can be made into heap. Note that

the new element at the root may violate the heap

property. All that is needed to restore the heap

property.

Heapsort(A)

Buildheap(A)

for i  length[A] //down to 2

do swap A[1]  A[i]

heapsize[A]  heapsize[A] - 1

Heapify(A, 1)

Example: Convert the following array to a heap

Picture the array as a complete binary tree:

Heap Sort

• The heapsort algorithm consists of two phases:

  • build a heap from an arbitrary array
  • use the heap to sort the data

• To sort the elements in the decreasing order, use a min heap

• To sort the elements in the increasing order, use a max heap

Example of Heap Sort

Array A

Sorted:

Take out biggest

Move the last element

to the root