HeapAndHeapsort PDF

Embed Size (px)

Citation preview

  • 7/29/2019 HeapAndHeapsort PDF

    1/12

    Heaps and Heapsort

    1

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    2/12

    Heaps are used to efficiently implement twooperations:

    Insert

    Find Max

    Definition: A Heap is a complete binary tree witheach node satisfying the heap property:

    The key stored in the node is greater than or equalto both keys stored in its children, if any.

    3

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    3/12

    10

    20

    17

    9

    10

    11

    4

    5

    3

    7

    5

    1

    2

    3

    4

    5

    6

    7

    8

    9

    3 7 5

    10 11 4 5

    917

    201

    2

    3

    4

    56 7

    89 10

    Heap example

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    4/12

    The heap data structure supports the followingoperations

    delete_max[H]: returns and deletes max.

    insert[H,x]: insert x into H.

    delete[H,i]: delete x from H.

    makeheap[A]: Transform an array into a heap.

    We restrict our discussion to max-heaps.

    5

    Heap Operations

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    5/12

    In a max-heap, if the value at a node becomes lessthan the key of any of its children, the heapproperty can be restored by swapping the current

    node and the child with maximum key value,repeating this process if necessary until

    the key at the node is greater than or equal to

    the keys of both children.

    we reach a leaf.

    9

    Sift Down Operation

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    6/12

    Procedure Sift-DownInput: H[1..n], i where 1 in.Output: H[i] is percolated down, if needed, so

    that its not smaller than its children.

    done := false;if (2*i) > nthen exit; { is a leaf node }repeat i = 2*i; if (i+1) n and key(H[i+1]) > key(H[i]) then i := i+1; if key(H[i/2]) < key(H[i]) then

    swap(H[i],H[i/2]); else done := true; end if;until 2*i > n or done;

    10

    Algorithm for sift down

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    7/12

    11

    3 7

    510

    11

    4 5

    9

    3

    20

    Example of sift down

    In Heap example, change 17 to 3. Then 3 will be sifted down.

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    8/12

    Work from high end of array to low end. Call SiftDown for each item. Dont need to callSiftDown on leaf nodes.

    AlgorithmMake-Heap

    Input: Array A[1..n] of n elements

    Output: Max-heap A[1..n]

    fori= n/2downto 1

    SiftDown(A,i);

    endfor;

    15

    Make-heap operation

    (Transform a given array into a heap)

    Time complexity to make a heap is O(n).Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    9/12

    16

    10

    4

    3

    8

    10

    11

    13

    7

    30

    17

    26

    1

    2

    3

    4

    5

    6

    7

    8

    9

    1

    3 8

    2

    4

    5

    3

    1110

    109

    17

    8

    30

    77

    613

    26

    4

    Example of make-heap

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    10/12

    AlgorithmHeapsort

    Input: Array A[1..n] of n elements

    Output:A[1..n]sorted.

    Make-Heap[A]

    forj= ndownto 2

    Interchange A[1] and A[j]

    SiftDown(A[1..j-1],i);

    endfor;

    Heapsort

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    11/12

    There are (n-1) iterations. Each iteration executes thesift-down operation.

    The cost of one sift-down is O(log n).

    Hence, the time complexity of heapsort is O(n log n).

    17

    Time complexity of Heapsort

    Friday, September 30, 11

  • 7/29/2019 HeapAndHeapsort PDF

    12/12

    Example of Heapsort

    Sort the array: 3 2 5 4 1

    Friday, September 30, 11