6 views

Uploaded by Ilakkiya Jayamoorthy

- presentation on heapsort
- (Algorithm) Definition: A Sort Algorithm That Splits
- Heap Sort
- Lecture 6
- 7. Heap Sort and Quick Sort-2
- Pertemuan 2 Algoritma
- A Survey of Adaptive QuickSort Algorithms
- HW3
- Sasmsfall07 Sheap
- Emphasis
- The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
- Sapiens: A Brief History of Humankind
- Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
- Shoe Dog: A Memoir by the Creator of Nike
- The Library Book
- The Unwinding: An Inner History of the New America
- Never Split the Difference: Negotiating As If Your Life Depended On It
- Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
- Yes Please
- A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story

You are on page 1of 30

Chapter 6 Heapsort

2

Introduction

Heapsort

Running time: O(n lg n)

Like merge sort

Sort in place: only a constant number of array elements are stored

outside the input array at any time

Like insertion sort

Heap

A data structure used by Heapsort to manage information during the

execution of the algorithm

Can be used as an efficient priority queue

3

Heaps

4

Binary Heap

An array object that can be viewed as a nearly complete

binary tree (see Section B.5.3)

Each tree node corresponds to an array element that stores the

value in the tree node

The tree is completely filled on all levels except possibly the lowest,

which is filled from the left up to a point

A has two attributes

length[A]: # of elements in the array

heap-size[A]: # of elements in the heap stored within A

heap-size[A] s length[A]

No element past A[heap-size[A]] is an element of the heap

max-heap and min-heap

5

A Max-Heap

6

Length and Heap-Size

11 7

7 11

Length = 10

Heap-Size = 7

7

Heap Computation

Given the index i of a node, the indices of its parent, left

child, and right child can be computed simply:

1 2 : ) (

2 : ) (

2 / : ) (

+ i return i RIGHT

i return i LEFT

i return i PARENT

8

Heap Property

Heap property the property that the values in the node

must satisfy

Max-heap property: for every node i other than the root

A[PARENT(i)] > A[i]

The value of a node is at most the value of its parent

The largest element in a max-heap is stored at the root

The subtree rooted at a node contains values on larger than that

contained at the node itself

Min-heap property: for every node i other than the root

A[PARENT(i)] s A[i]

9

Heap Height

The height of a node in a heap is the number of edges on

the longest simple downward path from the node to a leaf

The height of a heap is the height of its root

The height of a heap of n elements is O(lg n)

10

Heap Procedures

MAX-HEAPIFY: maintain the max-heap property

O(lg n)

BUILD-MAX-HEAP: produces a max-heap from an

unordered input array

O(n)

HEAPSORT: sorts an array in place

O(n lg n)

MAX-HEAP-INSERT, HEAP-EXTRACT, HEAP-INCREASE-

KEY, HEAP-MAXIMUM: allow the heap data structure to be

used as a priority queue

O(lg n)

11

Maintaining the Heap

Property

MAX-HEAPIFY

Inputs: an array A and an index i into the array

Assume the binary tree rooted at LEFT(i) and RIGHT(i) are max-

heaps, but A[i] may be smaller than its children (violate the max-

heap property)

MAX-HEAPIFY let the value at A[i] floats down in the max-heap

12

Example of MAX-HEAPIFY

13

MAX-HEAPIFY

Extract the indices of LEFT and RIGHT

children of i

Choose the largest of A[i], A[l], A[r]

Float down A[i] recursively

14

Running time of MAX-

HEAPIFY

O(1) to find out the largest among A[i], A[LEFT(i)], and

A[RIGHT(i)]

Plus the time to run MAX-HEAPIFY on a subtree rooted at

one of the children of node i

The childrens subtrees each have size at most 2n/3 the worst

case occurs when the last row of the tree is exactly half full

T(n) s T(2n/3) + O(1)

By case 2 of the master theorem: T(n) = O(lg n)

7/11

15

Building A Heap

16

Build Max Heap

Observation: A[(n/2+1)..n] are all leaves of the tree

Each is a 1-element heap to begin with

Upper bound on the running time

O(lg n) for each call to MAX-HEAPIFY, and call n times O(n lg n)

Not tight

17

18

Loop Invariant

At the start of each iteration of the for loop of lines 2-3, each

node i+1, i+2, .., n is the root of a max-heap

Initialization: Prior to the first iteration of the loop, i = n/2. Each

node n/2+1, n/2+2,.., n is a leaf and the root of a trivial max-heap.

Maintenance: Observe that the children of node i are numbered

higher than i. By the loop invariant, therefore, they are both roots of

max-heaps. This is precisely the condition required for the call MAX-

HEAPIFY(A, i) to make node i a max-heap root. Moreover, the MAX-

HEAPIFY call preserves the property that nodes i+1, i+2, , n are all

roots of max-heaps. Decrementing i in the for loop update

reestablishes the loop invariant for the next iteration

Termination: At termination, i=0. By the loop invariant, each node 1,

2, , n is the root of a max-heap. In particular, node 1 is.

19

Cost for Build-MAX-HEAP

Heap-properties of an n-element heap

Height = lg n

At most n/2

h+1

( nodes of any height h

) ( )

2

( )

2

( ) (

2 0

lg

0

lg

0

1

n O

h

n O

h

n O h O

n

h

h

n

h

h

n

h

h

=

(

(

(

= = =

+

Ignore the constant

2

)

2

1

1 (

2

1

2

2

0

=

= h

h

h

2

0 ) 1 ( x

x

kx

h

k

=

(for |x| < 1)

20

The HeapSort Algorithm

21

Idea

Using BUILD-MAX-HEAP to build a max-heap on the input

array A[1..n], where n=length[A]

Put the maximum element, A[1], to A[n]

Then discard node n from the heap by decrementing heap-size(A)

A[2..n-1] remain max-heaps, but A[1] may violate

call MAX-HEAPIFY(A, 1) to restore the max-heap property for

A[1..n-1]

Repeat the above process from n down to 2

Cost: O(n lg n)

BUILD-MAX-HEAP: O(n)

Each of the n-1 calls to MAX-HEAPIFY takes time O(lg n)

22

Example of HeapSort

Next Slide

23

14

1

14

10

1

14

10

9

1

Example of HeapSort (Cont.)

24

Algorithm

25

Priority Queues

26

Definition

A priority queue is a data structure for maintaining a set S of

elements, each with an associated value called a key. A

max-priority queue supports the following operations:

INSERT(S, x) inserts the element x into the set S

MAXIMUM(S) returns the element of S with the largest key

EXTRACT-MAX(S) removes and returns the element of S with the

largest key

INCREASE-KEY(S, x, k) increases the value of element xs key to

the new value k, which is assumed to be at least as largest as xs

current key value

Application of max-priority queue: Job scheduling in

computer

27

HEAP-MAXIMUM and

HEAP-EXTRACT-MAX

O(1)

O(lg n)

28

HEAP-INCREASE-KEY

Steps

Update the key of A[i] to its new value

May violate the max-heap property

Traverse a path from A[i] toward the root to find a proper place for

the newly increased key

O(lg n)

29

Example of HEAP-INCREASE-KEY

30

MAX-HEAP-INSERT

O(lg n)

- presentation on heapsortUploaded byabhics006
- (Algorithm) Definition: A Sort Algorithm That SplitsUploaded bysatishpatel_22
- Heap SortUploaded byhermione131911108
- Lecture 6Uploaded bySatyanshu Srivastava
- 7. Heap Sort and Quick Sort-2Uploaded byAngad pal
- Pertemuan 2 AlgoritmaUploaded bykiky
- A Survey of Adaptive QuickSort AlgorithmsUploaded byAI Coordinator - CSC Journals
- HW3Uploaded byNikola Tristani
- Sasmsfall07 SheapUploaded byshanthisec
- EmphasisUploaded bySonu Chethan

- Database Management System AssignmentUploaded byRohit Sharma
- SC2012 OpsMgr AuthoringUploaded bySantosh Kumar
- 1 Ssis Advanced Part2 m1 SlidesUploaded byroderinde
- Data Structures and Algorithms ArraysUploaded byHisham Thorakkal
- 02PerformanceProblems-1Uploaded bysnkhatri
- Computer for CompetetionUploaded byChandresh Kumar
- Patofisiologi DemamUploaded byAjes Cool
- Vmware QuestionsUploaded byHarish Kalyanaraman
- How to Graph Your NetworkUploaded byKenMiller
- Example Copy of ETS Service Catalog TemplateUploaded bySatish Kumar
- Magnetic RecordingUploaded byKatypearl
- DB2 (mainframe)Uploaded bynaveen_1507
- StacksUploaded byAsafAhmad
- unit 3-coUploaded byPriyanka H V
- LaserOS Sample Boot LoaderUploaded byddie8000
- BRKAPP-3006Uploaded byktn666
- M95_ATC_V1.1Uploaded byJeremy Woods
- Building Data Warehouses Using Ope n Source TechnologiesUploaded byStefanny Gutierrez
- Create Accounting Entries Troubleshooting Guide.docsOnERP.comUploaded byriazahmad82
- HelinksUploaded byjoaobolx
- embedded systems unit 8 notesUploaded byPradeep Kumar Goud Nadikuda
- Chapter 01 Address Book ServiceUploaded byMohamed Rafeek
- IndexingUploaded byStephen Efange
- cacheserve-datasheet.pdfUploaded byShweta Gupta
- Basic Computer aptitude Question & answers – Fundamental.pdfUploaded bybhulakshmidevi
- Blender.crashUploaded bymusamikailg
- Semiconductor Memories 1Uploaded byTB
- ch3 jaccardUploaded byAlan
- Java Lab ProgramsUploaded byV.KARTHIKEYAN.
- Recvr Point Splitter Enginuity 5876Uploaded byImtiazuddin Mohammed