Loading in 5 sec....

Design and Analysis of Algorithms HeapsortPowerPoint Presentation

Design and Analysis of Algorithms Heapsort

- 66 Views
- Uploaded on
- Presentation posted in: General

Design and Analysis of Algorithms Heapsort

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Design and Analysis of AlgorithmsHeapsort

HaidongXue

Fall 2013, at GSU

- A complete binary tree, and …

every level is completely filled,except possibly the last, which is filled from left to right

Yes

No

Yes

- Satisfy max-heap property: parent >= children

16

14

10

8

7

9

3

2

Since it is a complete tree, it can be put into an array without lose its structure information.

16

14

10

8

7

9

3

2

1

2

3

5

7

8

4

6

- Use an array as a heap

1

For element at i:

Parent index =parent(i)= floor(i/2);

Left child index = left(i)=2*i;

Right child index =right(i)=2*i +1

Last non-leaf node = floor(length/2)

16

2

3

14

10

4

5

6

7

8

7

9

3

8

i=3

2

floor(i/2)=floor(1.5)=1

2*i = 6

2*i+1=7

floor(length/2)=4

7

9

3

2

14

16

10

8

1

2

3

5

7

8

4

6

- Input: A compete binary tree A, rooted at i, ended at t, whose left and right sub trees are max-heaps; last node index
- Output: A max-heap rooted at i.
- Algorithm:
MAX-HEAPIFY (A, i, t)

1. if(right(i)>t and left(i)>t) return;

2. Choose the largest node among node i, left(i), right(i) .

3. if(the largest node is not i){

- m = the index of the larger node
- Exchange i with the largest node
- MAX-HEAPIFY (A, m, t)
}

2

16

10

14

7

9

3

8

- Input: a max-heap in array A
- Output: a sorted array A
HEAP-SORT Algorithm:

1. BUILD-MAX-HEAP(A)

2. Last node index i = A’s last node index

3. From the last element to the second in A{

exchange (i, root);

i--;

MAX-HEAPIFY(A, root, i);

}

16

14

10

3

9

8

7

2

14

8

10

3

9

2

7

16

10

8

9

14

3

2

7

16

9

8

3

14

10

2

7

16

8

7

3

14

10

2

9

16

7

2

3

14

10

8

9

16

3

2

7

14

10

8

9

16

- Input: a array A
- Output: a Max-Heap A
BUILD-MAX-HEAP(A):

Considering A as a complete binary tree, from the last non-leaf node to the first one i{

MAX-HEAPIFY(A, i, A.lastIndex);

}

7

10

8

16

14

14

8

16

10

7

- Input: array A
- Output: sorted array A
Algorithm:

1. BUILD-MAX-HEAP(A)

2. Last node index i = A’s last node index

3. From the last element to the second in A{

exchange (i, root);

i--;

MAX-HEAPIFY(A, root, i);

}

- Input: array A
- Output: sorted array A
Algorithm:

1. BUILD-MAX-HEAP(A)

2. Last node index i = A’s last node index

3. From the last element to the second in A{

exchange (i, root);

i--;

MAX-HEAPIFY(A, root, i);

}

O(nlgn) (or O(n) see page 157-159 for why)

O(n)

O(nlgn)