0% found this document useful (0 votes)
4 views

Lecture 14

Uploaded by

leezhipheng1049
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 14

Uploaded by

leezhipheng1049
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

CSC2103 - Data Structures & Algorithms

Heap Sort

1
Heap
A heap is a data structure that stores a
collection of objects, 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 Sort

A heap is a “complete” binary tree, usually


represented as an array:

16

14 10

8 7 9 3

2 4 1

A = 16 14 10 8 7 9 3 2 4 1
3

CSC2103 - Data Structures & Algorithms


Heap Sort
 Heap sort is a much more efficient
version of selection sort.
 It also works by finding the largest (or

smallest) element of the list, placing


that at the end (or beginning) of the list
 by using a data structure called a heap

[ a special type of binary tree]


 Once the data list has been made into

a heap the root node is guaranteed to


be the largest element. 4

CSC2103 - Data Structures & Algorithms


Heap Sort
 Theroot 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 2i
RIGHT (i)
5
return 2i + 1
CSC2103 - Data Structures & Algorithms
Heap Sort
heap:
 A balanced, left-justified binary tree in

which no node has a value greater


than the value in its parent
 Recall:
 The depth of a node is its distance from the
root
 The depth of a tree is the depth of the
deepest node
 A binary tree of depth n is balanced if all
the nodes at depths 0 through n-2 have 6
two children
CSC2103 - Data Structures & Algorithms
Heap Sort
Balanced tree

Balanced Balanced

Not balanced

CSC2103 - Data Structures & Algorithms


Heap order property
 Forevery 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.

 Inthis case the maximum value is


stored in the root
Heapify
 A node has the heap property if the value in the node
is as large as or larger than the values in its children
 All leaf nodes automatically have the heap property
 A binary tree is a heap if all nodes in it have the heap
property

10 10
10

7 4 7 13
7 10
colored node has colored node does not
colored node has
heap property have heap property
heap property

CSC2103 - Data Structures & Algorithms


Heapify
 If a node that does not have the heap property, you
can give it the heap property by exchanging its
value with the value of the larger child (Shifting up)

10 13

7 13 7 10
colored node does not colored node has
have heap property heap property

10

CSC2103 - Data Structures & Algorithms


Definition
 Max Heap
 Store data in ascending order
 Has property of

A[Parent(i)] ≥ A[i]
 Min Heap
 Store data in descending order
 Has property of

A[Parent(i)] ≤ A[i]
Max Heap Example
19

12 16

1 4 7

1 12 16 1 4 7
9
Array A
Min heap example
1

4 16

7 12 19

1 4 16 7 12 19

Array A
Insertion
 Algorithm
1. Add the new element to the next available
position at the lowest level
2. Restore the max-heap property if violated
 General strategy is percolate up (or bubble

up): if the parent of the element is smaller


than the element, then interchange the parent
and child.

OR

Restore the min-heap property if violated


 General strategy is percolate up (or bubble

up): if the parent of the element is larger than


the element, then interchange the parent and
child.
19 19

12 16 12 16

4 7 1 4 7 1
1
7
Insert 17
19

12 17
swap

1 4 7 1
6
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.
Heap Sort
A sorting algorithm that works by first
organizing the data to be sorted into a
special type of binary tree called a heap
Procedures on Heap
 Heapify

 BuildHeap
 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)
{
l  left(i)
r  right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest l
else largest  i
if r <= heapsize[A] and A[r] > A[largest]
then largest  r
if largest != i
then swap A[i]  A[largest]
Heapify(A, largest)
}
Build Heap
 We can use the procedure 'Heapify' in a bottom-up
fashion to convert an array A[1 . . n] into a heap. Since
the elements in the subarray A[n/2 +1 . . n] are all
leaves, the procedure BUILD_HEAP goes through the
remaining nodes of the tree and runs 'Heapify' on
each one. The bottom-up order of processing node
guarantees that the subtree rooted at children are
heap before 'Heapify' is run at their parent.

Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2 //down to 1
do Heapify(A, i)
}
Heap Sort Algorithm
 The heap sort algorithm starts by using procedure
BUILD-HEAP to build a heap on the input array A[1 . .
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

16 4 7 1 12 19

Picture the array as a complete binary tree:

16

4 7

1 12 19
16 16

4 7 4 19
swa
p

12 19 1 12 7
1

16 19
swa
p
12 19 12 16
swa
p

4 7 1 4 7
1
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

19

12 16

1 4 7
Example of Heap Sort
Take out biggest
19

12 16
Move the last element
to the root

1 4 7

Sorted:
Array A

12 1 1 4 7 19
6
7
swap
HEAPIFY()
12 16

1 4

Sorted:
Array A

7 12 1 1 4 19
6
16

12 7

1 4

Sorted:
Array A

1 12 7 1 4 19
6
Take out biggest
16
Move the last element
to the root
12 7

1 4

Sorted:
Array A

12 7 1 4 1 19
6
4

12 7

Sorted:
Array A

4 12 7 1 1 19
6
swap 4

HEAPIFY()
12 7

Sorted:
Array A

4 12 7 1 1 19
6
12

4 7

Sorted:
Array A

12 4 7 1 1 19
6
Take out biggest
12
Move the last
element to the
root 4 7

Sorted:
Array A

4 7 1 12 1 19
6
1
swap

4 7

Sorted:
Array A

1 4 7 12 1 19
6
7

4 1

Sorted:
Array A

7 4 1 12 1 19
6
Take out biggest
7
Move the last
element to the
4 1 root

Sorted:
Array A

1 4 7 12 1 19
6
swap 1

HEAPIFY()
4

Sorted:
Array A

4 1 7 12 1 19
6
Take out biggest
Move the last 4
element to the
root
1

Sorted:
Array A

1 4 7 12 1 19
6
Take out biggest
1

Sorted:
Array A

1 4 7 12 1 19
6
Sorted:

1 4 7 12 16 19
Time Analysis
 Build Heap Algorithm will run in O(n)
time
 There are n-1 calls to Heapify each call

requires O(log n) time


 Heap sort program combine Build

Heap program and Heapify, therefore


it has the running time of O(n log n)
time
 Total time complexity: O(n log n)
Possible Application
 When we want to know the task that carry
the highest priority given a large number
of things to do

 Intervalscheduling, when we have a lists


of certain task with start and finish times
and we want to do as many tasks as
possible

 Sorting a list of elements that needs and


efficient sorting algorithm
Heap Sort
Heapsort’s efficiency
 Makeheap is O(n log2 n)
 Siftdown is O(log2 n)
 Heapsortis O(n log2 n) + (n – 1) O(log2 n)
= O(n log2 n)

42

CSC2103 - Data Structures & Algorithms


Reference
 Deitel,P.J. and Deitel, H.M. (2008) “C+
+ How to Program”. 6th ed. Upper
Saddle River, New Jersey, Pearson
Education, Inc.
 Carrano, Frank M. (2007) “Data

Abstraction and problem solving


with C++: walls and mirrors”. 5th
ed. Upper Saddle River, New Jersey,
Pearson Education, Inc.
Visualization of Sort
 https://www.cs.usfca.edu/~

galles/visualization/ComparisonSort.ht
ml

44

CSC2103 - Data Structures & Algorithms

You might also like