0% found this document useful (0 votes)
172 views29 pages

Sorting Algorithms: Heap Sort

Heap sort works by building a max heap from the input array and then repeatedly extracting the maximum element from the heap and inserting it into the sorted end of the array. This is done in O(nlogn) time by performing n delete max operations, each taking O(logn) time. The example demonstrates heap sort on an array of numbers, building the max heap, deleting elements from the heap and inserting them into the sorted portion of the array until it is fully sorted.

Uploaded by

solanki06
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views29 pages

Sorting Algorithms: Heap Sort

Heap sort works by building a max heap from the input array and then repeatedly extracting the maximum element from the heap and inserting it into the sorted end of the array. This is done in O(nlogn) time by performing n delete max operations, each taking O(logn) time. The example demonstrates heap sort on an array of numbers, building the max heap, deleting elements from the heap and inserting them into the sorted portion of the array until it is fully sorted.

Uploaded by

solanki06
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

Chapter 7: Sorting Algorithms


Heap Sort
Lydia Sinapova, Simpson College

Heap Sort
Basic Idea Complexity Example Animation

Idea
Store N elements in a binary heap tree.

Perform delete_Min operation N times,

storing each element deleted from the heap into another array. Copy back the array.
Not very efficient to use two arrays.

Improvement use one array for the binary

heap and the sorted elements


3

Improvements
Use the same array to store the deleted elements instead of using another array After each deletion we get a vacant position in the array - the last cell. There we store the deleted element, which becomes part of the sorted sequence.
4

Improvements
When all the elements are deleted and stored in the same array following the above method, the elements will be there in reversed order. What is the remedy for this? Store the elements in the binary heap tree in reverse order of priority - then at the end the elements in the array will be in correct order.
5

Complexity
Sorts in O(NlogN) time by performing N times deleteMax operations. Each deleteMax operation takes log N running time. N times performing deleteMax NlogN running time

Used for general purpose sorting, guarantees O(N logN)


6

Example
15 19 10 7 17 16

1. Consider the values of the elements as priorities and build the heap tree. 2. Start deleteMax operations, storing each deleted element at the end of the heap array.
7

Example (cont)
Note that we use only one array , treating its parts differently:
when sorting, part of the array will be the heap, and the rest part - the sorted array

Build the Heap


We start with the element at position SIZE/2 comparing the item with the children. The hole is percolated down to position 6 and the item is inserted there.

15

19

hole

17

16

child

10

Result:
15 19 16 7 17 10
9

Build the Heap Next we compare position 2 with its children.


15
hole

16

child1

17

child2

10

19

19 is greater than 7 and 17, and we continue with position 1


15 19 16 7 17 10
10

Build the Heap Percolate down the hole at position 1


19
15

16

17

10

The hole at position 1 is percolated down to position 2 -the greater child.


19
15
11

16

17

10

Build the Heap Percolate down the hole at position 2


19
15

16

17

10

One of the children of the hole at position 2 - item 17, is greater than 15. So we percolate the hole to position 5.
19 17 16 7 15 10
12

Build the Heap


19 17 16 7
19 17 7 15 16 10
13

15

10

the heap is built

DeleteMax the top element 19

Sorting
15 19

17
10

16

Store the last heap element (10) in a temporary place. Move the DeletedMax element (19) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top

17
7 15

16

14

Percolate down the hole

Sorting
7 15 19
17

17 10

16

16
7 15
15

Percolate down the hole

Sorting
7 19
17 15 7
16

17 10

15

16

16

Fill the hole

Sorting
16 7 10 19
17 15 7 10
17

17

15

16

DeleteMax the top element 17

Sorting
17 19

15
10

16

Store the last heap element (10) in a temporary place. Move the DeletedMax element (17) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top

15 7

16

18

Percolate down the hole

Sorting
7 17 19 16
15 7
19

16
10

15

Fill the hole

Sorting
10 7 17 19 16
15 7
20

16

15

10

DeleteMax the top element 16

Sorting
17 19

15
7

10

16

Store the last heap element (7) in a temporary place. Move the DeletedMax element (16) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top

15

10

21

Percolate down the hole

Sorting
16 17 19
15 10

15
7

10

22

Fill the hole

Sorting
10 16 17 19
15

15

10

23

DeleteMax the top element 15

Sorting
17 19

7
10

15

16

Store the last heap element (10) in a temporary place. Move the DeletedMax element (15) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top

24

Percolate down the hole

Sorting
16 17 19

7
10

15

Since 10 is greater than the children of the hole, It has to be inserted in the hole

25

Fill the hole

Sorting
15 16 17 19
10

10

26

DeleteMax the top element 10

Sorting
17 19

10
7

15

16

Store the last heap element (7) in a temporary place. Move the DeletedMax element (10) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top

27

Fill the hole

Sorting
15 16 17 19
7

10

The hole has no children and so it has to be filled.

28

Sorted array
7 10 15 16 17 19

7 is the last element from the heap,


so now the array is sorted

29

You might also like