Top Banner
Procedures on Heap Heapify Build Heap Heap Sort
27

Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

Oct 03, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

Procedures on Heap

Heapify

Build Heap

Heap Sort

Page 2: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

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)

}

Page 3: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

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)

}

Page 4: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

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)

}

Page 5: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array 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

12 1 19

Page 6: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

16

4 7

12 1 19

16

4 19

12 1 7

16

12 19

4 1 7

19

12 16

4 1 7

swap

swap

swap

Page 7: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[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

4 1 7

Page 8: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

Example of Heap Sort

19

12 16

4 1 7

19 12 16 1 4 7

Array A Sorted:

Take out biggest

Move the last element

to the root

Page 9: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

12 16

4 1

7

19 12 16 1 4 7

Array A Sorted:

HEAPIFY()

swap

Page 10: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

12

16

4 1

7

19 12 16 1 4 7

Array A Sorted:

Page 11: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

12

16

4 1

7

19 12 16 1 4 7

Array A Sorted:

Take out biggest

Move the last element

to the root

Page 12: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

12

4

1

7

19 12 16 1 4 7

Array A Sorted:

Page 13: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

12

4

1

7

19 12 16 1 4 7

Array A Sorted:

HEAPIFY()

swap

Page 14: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

12

4

1

7

19 12 16 1 4 7

Array A Sorted:

Page 15: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

12

4

1

7

19 12 16 1 4 7

Array A Sorted:

Take out biggest

Move the last

element to the

root

Page 16: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

4

1

7

19 12 16 1 4 7

Array A Sorted:

swap

Page 17: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

4 1

7

19 12 16 1 4 7

Array A Sorted:

Page 18: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

4 1

7

19 12 16 1 4 7

Array A Sorted:

Move the last

element to the

root

Take out biggest

Page 19: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

4

1

19 12 16 1 4 7

Array A Sorted:

HEAPIFY()

swap

Page 20: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

4

1

19 12 16 1 4 7

Array A Sorted:

Move the last

element to the

root

Take out biggest

Page 21: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

1

19 12 16 1 4 7

Array A Sorted:

Take out biggest

Page 22: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

19 12 16 1 4 7

Sorted:

Page 23: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

Heap Sort using STL

#include <iostream> #include <algorithm> #include <vector> int main () { int array[] = {10,20,30,5,15,25,36}; vector<int> v(array, array+7); cout<<“\nThe Vector contains : “ for(int i=0;i<v.size();i++) { cout<< ‘ ‘ <<v[i] ; }

Output: Vector contains :

20 30 5 15 25 10 36

Page 24: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

Heap Sort using STL

cout<<“\n Heapify ……”;

make_heap (v.begin(),v.end()); cout << "initial max heap : " << v.front() << “\n”; cout<<“\nThe Vector contains after heapifying : “ for(int i=0;i<v.size();i++) { cout<< ‘ ‘ <<v[i] ; } Output: Heapify ……

initial max heap :36 The Vector contains after heapifying:

20 30 5 15 25 36 10

Page 25: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

sort_heap(v.begin( ),v.end());

pop_heap (v.begin(),v.end());

v.pop_back();

cout << "max heap after pop : " << v.front() << '\n';

Heap Sort using STL 10 15 20 25 30 5

Output: max heap after pop:36

36

10 15 20 25 30 36 5

10 15 20 25 30 36

Page 26: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

v.push_back(99);

push_heap (v.begin(),v.end());

std::cout << "max heap after push: " << v.front() << '\n';

Heap Sort using STL

Output: max heap after pop:99

10 15 20 25 30 36 99

15 10 25 30 36 20 99

Page 27: Procedures on Heap - WordPress.com … · 28/06/2016  · Heap Sort Algorithm The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 .

sort_heap (v.begin(),v.end()); std::cout << "final sorted range :"; for (unsigned i=0; i<v.size(); i++) std::cout << ' ' << v[i]; std::cout << '\n'; return 0; }

10 15 20 25 30 36 99