Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in...

29
Chapter 7: Chapter 7: Sor Sor ting ting A A lgorithms lgorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    282
  • download

    1

Transcript of Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in...

Page 1: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

Chapter 7: Chapter 7: SorSorting ting AAlgorithmslgorithms

Heap Sort

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

Lydia Sinapova, Simpson College

Page 2: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

2

Heap Sort

Basic IdeaComplexityExampleAnimation

Page 3: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

3

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

Page 4: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

4

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.

Page 5: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

5

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.

Page 6: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

6

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)

Page 7: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

7

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.

Page 8: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

8

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

Page 9: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

9

Build the Heap

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

15 19 7 17 16

10

Result:

15 19 16 7 17 10

hole child

Page 10: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

10

Build the Heap

Next we compare position 2 with its children.

15 16 7 17 10

19

hole child1 child2

19 is greater than 7 and 17,

and we continue with position 1

15 19 16 7 17 10

Page 11: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

11

Build the Heap

Percolate down the hole at position 1

19 16 7 17 10

15

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

19 16 7 17 10

15

Page 12: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

12

Build the Heap

Percolate down the hole at position 2

19 16 7 17 10

15

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

Page 13: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

13

Build the Heap

19 17 16 7 15 10

19

17 16

7 15 10

the heap is built

Page 14: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

14

SortingDeleteMax the top element 19

17 16 7 15 19

17 16

7 15

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

10

Page 15: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

15

SortingPercolate down the hole

16 7 15 19

16

7 15

10

17

17

Page 16: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

16

SortingPercolate down the hole

16 715 19

16

7

15

10

17

17

Page 17: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

17

SortingFill the hole

16 715 19

10

16

7

15

1017

17

Page 18: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

18

SortingDeleteMax the top element 17

16 715 19

16

7

15

17

10

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

Page 19: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

19

SortingPercolate down the hole

16 715 19

16

7

15

17

10

Page 20: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

20

SortingFill the hole

16 715 19

10

16

7

15

1710

Page 21: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

21

SortingDeleteMax the top element 16

1615 19

1015

1710

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

7

Page 22: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

22

SortingPercolate down the hole

1615 19

10

15

1710

7

Page 23: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

23

SortingFill the hole

1615 19

107

15

17107

Page 24: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

24

SortingDeleteMax the top element 15

1615 19

7

17

10

7

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

Page 25: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

25

SortingPercolate down the hole

1615 19

7

17

10

7

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

Page 26: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

26

SortingFill the hole

1615 19

7

10

1710 7

Page 27: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

27

SortingDeleteMax the top element 10

1615 1917

7

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

10

Page 28: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

28

SortingFill the hole

1615 19

7

177 10

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

Page 29: Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.

29

Sorted array

1615 19177 10

7 is the last element from the heap,

so now the array is sorted