05 - 18 jan - the sorting wrap-up

35
CS 321. Algorithm Analysis & Design Lecture 5 The Sorting Wrap-Up

Upload: neeldhara-misra

Post on 14-Apr-2017

72 views

Category:

Education


2 download

TRANSCRIPT

Page 1: 05 - 18 Jan - The Sorting Wrap-Up

CS 321. Algorithm Analysis & Design Lecture 5

The Sorting Wrap-Up

Page 2: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

Page 3: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

Page 4: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

1 2

Page 5: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

1 2

13 8 5 6

Page 6: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

1 2

13 8 5 6

3 12 11 7 16

Page 7: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

1 2

13 8 5 6

3 12 11 7 16

Page 8: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

1 2

13 8 5 6

3 12 11 7 16

Page 9: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

1 2

13 8 5 6

3 12 11 7 16

Page 10: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

1 2

13 8

5

6

3 12 11 7

16

Page 11: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 56 3 12 11 716

19

1 2

13 8

5

6

3 12 11 7

16

Page 12: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 56 3 12 11 716

19

1 2

13 8

5

6

3 12 11 7

16

Page 13: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 56 3 1211 716

19

1 2

13

8 5

6

3 12

11

7

16

Page 14: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 56 3 1211 716

19

1 2

13

8 5

6

3 12

11

7

16

Page 15: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 56 3 1211 716

19

1 2

13

8 5

6

3 12

11

7

16

Page 16: 05 - 18 Jan - The Sorting Wrap-Up

19 1 213 8 56 3 1211 716

19

1

213

8 5

6

3 12

11

7

16

Page 17: 05 - 18 Jan - The Sorting Wrap-Up

19 1 213 85 6 3 1211 716

19

1

2

13

8

5 6

3 12

11

7

16

Page 18: 05 - 18 Jan - The Sorting Wrap-Up

19 1 213 85 6 3 1211 716

19

1

2

13

8

5 6

3 12

11

7

16

Page 19: 05 - 18 Jan - The Sorting Wrap-Up

19 1 213 85 6 3 1211 716

19

1

2

13

8

5 6

3 12

11

7

16

Page 20: 05 - 18 Jan - The Sorting Wrap-Up

19 1 213 85 6 312 11 716

19

1 2

13

8

5 6

3

12 11

7

16

Page 21: 05 - 18 Jan - The Sorting Wrap-Up

19 1 213 85 6 312 11 716

19

1 2

13

8

5 6

3

12 11

7

16

Page 22: 05 - 18 Jan - The Sorting Wrap-Up
Page 23: 05 - 18 Jan - The Sorting Wrap-Up

Node at height h has to move at most h steps to find its place.

Page 24: 05 - 18 Jan - The Sorting Wrap-Up

Node at height h has to move at most h steps to find its place.

Number of nodes at height h?

Page 25: 05 - 18 Jan - The Sorting Wrap-Up

Node at height h has to move at most h steps to find its place.

Number of nodes at height h?

2(log n - h)

Page 26: 05 - 18 Jan - The Sorting Wrap-Up

Node at height h has to move at most h steps to find its place.

Number of nodes at height h?

2(log n - h)

Overall running time (summed over h):

Page 27: 05 - 18 Jan - The Sorting Wrap-Up

Node at height h has to move at most h steps to find its place.

Number of nodes at height h?

2(log n - h)

n(h/2(h))

Overall running time (summed over h):

Page 28: 05 - 18 Jan - The Sorting Wrap-Up

Node at height h has to move at most h steps to find its place.

Number of nodes at height h?

2(log n - h)

n(h/2(h))

Overall running time (summed over h):

Heapify: Turns out to be a linear effort overall.

Page 29: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

13 16

12 11 5 6

3 1 8 7 2

Page 30: 05 - 18 Jan - The Sorting Wrap-Up

19 1 2 13 8 5 6 3 12 11 7 16

19

13 16

12 11 5 6

3 1 8 7 2

To sort, repeatedly extract the

maximum element to the end of the array.

An O(n log n) effort.

Page 31: 05 - 18 Jan - The Sorting Wrap-Up

Finding Medians

Page 32: 05 - 18 Jan - The Sorting Wrap-Up

Finding Mediansusing heaps

Page 33: 05 - 18 Jan - The Sorting Wrap-Up

19

13 16

12 11 5 6

3 1 8 7 2

19 13 16 12 11 5 6 3 1 8 7 2

Page 34: 05 - 18 Jan - The Sorting Wrap-Up
Page 35: 05 - 18 Jan - The Sorting Wrap-Up

Store the first (n/2) elements in a max-heap.

Store the last (n/2) elements in a min-heap.