HEAPSORT - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

HEAPSORT

Description:

Build the sub-array A[p] .. A[q] into a heap: void heapify(int p,int q) { int c = 2 * p; ... Then we do the 'exchange' to put the max element at the end of the array. ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 17
Provided by: curryA
Category:

less

Transcript and Presenter's Notes

Title: HEAPSORT


1
HEAPSORT
  • The array A1 .. An is sorted by treating the
    sub-array A1 .. Ap as a heap1. Build A1
    .. Ap into a heap.2. Exchange A1 and Ap,
    so that Ap is now the maximum element.Repeat 1
    and 2 for p n, n-1, ... , 3, 2.

2
COMPLETE BINARY TREES
  • A complete binary tree is a rooted binary tree in
    which each parent node has exactly two children
    and all the leaves are at the last level.

3
Complete Binary Trees
  • Example

Level 0
Level 1
Level 2
Level 3
4
ALMOST COMPLETE BINARY TREES
  • An almost complete binary tree is a rooted binary
    tree with levels 0 to k, in which all nodes in
    levels 0 to (k-1) are present and only the
    rightmost nodes in level k may be absent.
  • A complete binary tree is an almost complete
    binary tree.

5
Almost Complete Binary Trees
  • Example

Level 0
Level 1
Level 2
Level 3
6
HEAPS
  • A heap is an almost complete binary tree in which
    each node x is assigned a value v(x) that
    satisfies the heap propertyv(x) max
    v(lchild(x)), v(rchild(x))

7
Heaps
  • Example

100
100
98
75
85
15
99
92
83
20
74
50
8
ARRAYS AS ALMOST COMPLETE BINARY TREES
  • An array A1 .. An can be treated as an almost
    complete binary tree, as followsA1 is the
    rootA2 and A3 are the children of
    A1A4 and A5 are the children of
    A2A6 and A7 are the children of A3etc.

9
Arrays as Almost Complete Binary Trees
  • Array elements as nodes

A1
A3
A2
A4
A5
A7
A6
A10
A12
A11
A9
A8
10
Arrays as Almost Complete Binary Trees
  • Assume an array A1 .. An.
  • For a parent node at array index klchild(k) 2
    krchild(k) 2 k 1provided that 2k n
    and 2k1 n.
  • For a child node at array index kparent(k)
    ?k/2?

11
HEAPIFY
  • Build the sub-array Ap .. Aq into a heap
  • void heapify(int p,int q) int c 2 p if(c
    lt q) if(c1 lt q Ac lt Ac1)
    c if(Ap lt Ac) swap(c,p)
    heapify(c,q)

12
Nonrecursive Heapify
  • void heapify(int p, int q) int c for(
    c2p, c lt q pc) if(c1 lt q
    Ac lt Ac1) c if(Ap gt Ac)break
    exchange Ac and Ap

13
BUILD HEAP
  • Initially, we build the entire array A1 .. An
    into a heap.
  • void buildheap(void) int j for(jn/2 j gt
    1 --j) heapify(j,n)

14
HEAPSORT
  • Then we do the exchange to put the max element
    at the end of the array.
  • void heapifyexchange(void) int j for(jn j
    gt 2 --j) heapify(1,j) exchange(1,j)

15
Final Heapsort Code
  • We put everything together to get heapsort
  • main() buildheap() heapify_exchange()

16
Running Time of Heapsort
  • Buildheap() runs in O(n lg(n)) time.heapify_excha
    nge() runs in O(n lg(n)) time.
  • Thus heapsort main() runs in O(n lg(n)) time.
Write a Comment
User Comments (0)
About PowerShow.com