Introduction to Heapsort and Heap Properties, Slides of Computer Science

An overview of heapsort, a divide and conquer sorting algorithm, and discusses the properties of heaps. Topics include the master theorem, heap structure as a complete binary tree, heap operations such as heapify(), and the advantages of heapsort.

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
Introduction to heapsort
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Introduction to Heapsort and Heap Properties and more Slides Computer Science in PDF only on Docsity!

Algorithms

Introduction to heapsort

Review: The Master Theorem

 Given: a divide and conquer algorithm

 An algorithm that divides the problem of size n

into a subproblems, each of size n / b

 Let the cost of each stage (i.e., the work to divide

the problem + combine solved subproblems) be

described by the function f (n)

 Then, the Master Theorem gives us a

cookbook for the algorithm’s running time:

Sorting Revisited

 So far we’ve talked about two algorithms to

sort an array of numbers

 What is the advantage of merge sort?

 Answer: O(n lg n) worst-case running time

 What is the advantage of insertion sort?

 Answer: sorts in place
 Also: When array “nearly sorted”, runs fast in practice

 Next on the agenda: Heapsort

 Combines advantages of both previous algorithms

 A heap can be seen as a complete binary tree:

 What makes a binary tree complete?

 Is the example above complete?

Heaps 16 14 10 8 7 9 3 2 4 1

Heaps

 In practice, heaps are usually implemented as

arrays:

16 14 10 8 7 9 3 2 4 1

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

Heaps

 To represent a complete binary tree as an array:

 The root node is A[1]

 Node i is A[ i ]

 The parent of node i is A[ i /2] (note: integer divide)

 The left child of node i is A[2 i ]

 The right child of node i is A[2 i + 1]

16 14 10 8 7 9 3 2 4 1

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

The Heap Property

 Heaps also satisfy the heap property :

A[ Parent ( i )] ≥ A[ i ] for all nodes i > 1

 In other words, the value of a node is at most the

value of its parent

 Where is the largest element in a heap stored?

Heap Height

 Definitions:

 The height of a node in the tree = the number of

edges on the longest downward path to a leaf

 The height of a tree = the height of its root

 What is the height of an n-element heap? Why?

 This is nice: basic heap operations take at most

time proportional to the height of the heap

Heap Operations: Heapify() Heapify(A, i) { l = Left(i); r = Right(i); if (l <= heap_size(A) && A[l] > A[i]) largest = l; else largest = i; if (r <= heap_size(A) && A[r] > A[largest]) largest = r; if (largest != i) Swap(A, i, largest); Heapify(A, largest); }

Heapify() Example 16 4 10 14 7 9 3 2 8 1 A = 16 4 10 14 7 9 3 2 8 1

Heapify() Example 16 4 10 14 7 9 3 2 8 1 A = 16 4 10 14 7 9 3 2 8 1

Heapify() Example 16 14 10 4 7 9 3 2 8 1 A = 16 14 10 4 7 9 3 2 8 1

Heapify() Example 16 14 10 4 7 9 3 2 8 1 A = 16 14 10 4 7 9 3 2 8 1

Heapify() Example 16 14 10 8 7 9 3 2 4 1 A = 16 14 10 8 7 9 3 2 4 1