04 - 15 Jan - Heap Sort

Preview:

Citation preview

CS 321. Algorithm Analysis & Design Lecture 4

Heap Sort

4 9 2 1 8 5 6

4 9 2 1 8 5 6

4 9 2

1

8 5 6

4 9 2

1

8 5 6

4 9

21

8 5 6

4 9

21

8 5 6

4

9

21

8 5 6

4

9

21

8 5 6

4

9

21

8

5

6

4

9

21

8

5

6

4

9

21

8

5 6

4

9

21

8

5 6

4

9

21 85 6

4

9

21 85 6

4 921 85 6

4 921 85 6

Can we speed up the find-min operations?

4

921

85

6

Organize the elements so that finding minimum is “easy”.

4

921

85

6

Organize the elements so that deleting the minimum element is “easy”.

Store a set of n distinct numbers.

Inserting a new element and deleting the minimum element can be done in time proportional to (log n).

GOAL

All jumbled up

4 9 2 1 8

All jumbled up

4 9 2 1 8 3

All jumbled up

4 9 2 1 8 3 6

All jumbled up

4 9 2 1 8 3

Insert: O(1)

6

All jumbled up

4 9 2 1 8 3

Insert: O(1) Delete-Min: O(n)

6

All sorted

9 8 6 3 1

All sorted

9 8 6 3

All sorted

9 8 6 3

Delete-Min: O(1)

All sorted

9 8 6 3

Delete-Min: O(1)

4

All sorted

9 8 6 3

Delete-Min: O(1)

47

All sorted

9 8 6 3

Insert: O(n) Delete-Min: O(1)

47

In-between?

Invariant: The first element of the array is the minimum.

In-between?

Invariant: The first element of the array is the minimum.

In-between?

Invariant: The first element of the array is the minimum.

In-between?

Invariant: The first element of the array is the minimum.

In-between?

Invariant: The first element of the array is the minimum.

In-between?

Invariant: The first element of the array is the minimum.

In-between?

Invariant: The first element of the array is the minimum.

In-between?

Invariant: The first element of the array is the minimum.

4

921

85

6

6

1

59

24

8

x is a child of y: x is larger than y.

6

1

59

24

8

6

1

59

24

8

6

1

59

24

8

61 5924 8

61 5924 8

6

1

59

24

8

61 5924 8

6

1

59

24

8

1

2 3

4 5 6 7

1 2 3 4 5 6 7

61 5924 8

6

1

59

24

8

1

2 3

4 5 6 7

1 2 3 4 5 6 7

left-child(i) = 2i

61 5924 8

6

1

41

2

4

1 2 3 4 5 6 7

61 5924 8

6

1

41

2

4

1 2 3 4 5 6 7

left-child(i) = 2i

left-child(i) = 2i

left-child(i) = 2i

i

2i

left-child(i) = 2i

i+k

2i + 2k

i

2i

6

1

59

24

8

Delete Min

6 1

5

9

24

8

Delete Min

6 1

5

9

24

8

Delete Min

6 1

5

9

24

8

Delete Min

6 1

5

9

24

8

Delete Min

6 1

5

9

24

8

Delete Min

6 1

5

9

2

4

8

Delete Min

6 1

2

9

54

8

Delete Min

6

2

9

54

8

Insert

6

3

2

9

54

8

Insert

6 3

2

9

54

8

Insert

6 3

2

9

54

8

Insert

6

3

2

9 5

4

8

Insert

6

3

2

9 5

4

8

Insert

Heapify

Recommended