heap_sort jkghjkhef

Upload: rayedkhan

Post on 03-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 HEAP_SORT jkghjkhef

    1/16

    Session: Jan May 2013

    EEC 012 Data Structures 1Prof. (Dr) N. Guruprasad

    HEAP TREE & HEAP SORT

  • 7/28/2019 HEAP_SORT jkghjkhef

    2/16

    Session: Jan May 2013

    EEC 012 Data Structures 2

    Heap tree : defined as binary tree with keysassigned to nodes provided following two conditions

    are met :

    1) ( tree shape requirement )

    all levels are full exceptpossibly the last level; only some right most leaves may

    be missing.

    2) ( parental dominance requirement )key at each

    node is greater than or equal to keys at its children.

    Prof. (Dr) N. Guruprasad

  • 7/28/2019 HEAP_SORT jkghjkhef

    3/16

    Session: Jan May 2013

    EEC 012 Data Structures 3

    HEAP TREE

    10

    5 7

    4 2 1

    Prof. (Dr) N. Guruprasad

  • 7/28/2019 HEAP_SORT jkghjkhef

    4/16

    Session: Jan May 2013

    EEC 012 Data Structures 4

    Not HEAP - violates shape requirement

    10

    5 7

    2 1

    Prof. (Dr) N. Guruprasad

  • 7/28/2019 HEAP_SORT jkghjkhef

    5/16

    Session: Jan May 2013

    EEC 012 Data Structures 5

    Not HEAP - violates dominance requirement

    10

    5 7

    6 2 1

    Prof. (Dr) N. Guruprasad

  • 7/28/2019 HEAP_SORT jkghjkhef

    6/16

    Session: Jan May 2013

    EEC 012 Data Structures 6

    Applications :Interval scheduling we may have a list of tasks with

    certain start and finish times and we want to do as many of

    these tasks as possible in a given period of time.

    Priority queue scheduling in OS

    Event-driven simulators to maintain the list of events to be

    simulated in order of their time of occurrence.

    Prof. (Dr) N. Guruprasad

  • 7/28/2019 HEAP_SORT jkghjkhef

    7/16

    Session: Jan May 2013

    EEC 012 Data Structures 7

    Two variations :

    1) Max heapnode is greater than or equal to its

    children. ( descending heap )

    2) Min heapnode is smaller than or equal to its

    children. ( ascending heap )

    Construction of Heap :

    1) Bottom up heap construction2) Top down heap construction

    Prof. (Dr) N. Guruprasad

  • 7/28/2019 HEAP_SORT jkghjkhef

    8/16

    Session: Jan May 2013

    EEC 012 Data Structures 8

    Bottom up heap construction :

    Initializes a binary tree and heapifies as follows :

    1) Starting with last parental node, algorithm checks

    whether parental dominance holds for key at this

    node

    2) If it does not; algorithm exchanges nodes key K

    with larger key K of its children and checks whether

    parental dominance holds for K in its new position.3) This process continues until parental dominance

    requirement for K is satisfied.

    Prof. (Dr) N. Guruprasad

  • 7/28/2019 HEAP_SORT jkghjkhef

    9/16

    Session: Jan May 2013

    EEC 012 Data Structures 9

    4) After completing the Heapification ofsubtree rooted

    at current parental node, algorithm proceeds to do the

    same for nodes immediate predecessor.

    5) Algorithm stops after this is done for trees root.

    Prof. (Dr) N. Guruprasad

  • 7/28/2019 HEAP_SORT jkghjkhef

    10/16

    Session: Jan May 2013

    EEC 012 Data Structures 10

    Example : Construct a heap for the followingelements :

    2 9 7 6 5 8

    using bottom up technique.

    Prof. (Dr) N. Guruprasad

  • 7/28/2019 HEAP_SORT jkghjkhef

    11/16

    Session: Jan May 2013

    EEC 012 Data Structures 11

    Algorithm : HeapBottomUp(H[1..n])

    // Purpose : Construct heap from elements of given array by

    bottom up algorithm

    // Input : Array H[1..n] of orderable items

    // Output : Heap H[1..n]

    for i [ n/2 ] downto 1 do

    k Iv H[k]

    heap false

    Prof. (Dr) N. Guruprasad

    S

  • 7/28/2019 HEAP_SORT jkghjkhef

    12/16

    Session: Jan May 2013

    EEC 012 Data Structures 12

    while not heap and 2 * k n do

    j 2 * kif j < n

    if H[ j ] < H [ j+1 ]

    j j + 1

    if v H [ j ]

    heap true

    else

    H[k] H[j];k j

    H[ k ] v

    Prof. (Dr) N. Guruprasad

    S i J M 2013

  • 7/28/2019 HEAP_SORT jkghjkhef

    13/16

    Session: Jan May 2013

    EEC 012 Data Structures 13

    Top down heap construction :

    Constructs a heap by successive iterations.1) Attach a new node K after last leaf in existing heap.

    2) Shift K up to its appropriate place in new heap as

    follows :1) Compare K with parents key; if parent K; STOP tree

    is heap

    2) Otherwise swap these two keys and compare K with

    new parent.

    3) Swapping continues until K is not greater than its

    last parent or it reaches root.

    Prof. (Dr) N. Guruprasad

    S i J M 2013

  • 7/28/2019 HEAP_SORT jkghjkhef

    14/16

    Session: Jan May 2013

    EEC 012 Data Structures 14

    Example : Construct a heap for the following

    elements :

    50 25 30 75 100 45 80

    using top down approach.

    Prof. (Dr) N. Guruprasad

    Session Jan Ma 2013

  • 7/28/2019 HEAP_SORT jkghjkhef

    15/16

    Session: Jan May 2013

    EEC 012 Data Structures 15

    Heap sort :

    Interesting sorting algorithm by J.W.J. WILLIAMS.

    Basically a two stage algorithm that works as follows :

    Stage 1 ) ( Heap construction ) construct heap for given

    array.Stage 2 ) ( Maximum deletion ) apply root deletion

    operation n-1 times to remaining heap.

    NOTE : under array implementation of HEAP; element

    being deleted is placed last in an array.

    Prof. (Dr) N. Guruprasad

    Session: Jan May 2013

  • 7/28/2019 HEAP_SORT jkghjkhef

    16/16

    Session: Jan May 2013

    EEC 012 D t St t 16

    Example : Sort the following elements using HeapSort.

    13 86 43 38 54 23 08 63

    Solution : Two stage process :

    1) Using top down approach we build heap tree

    2) By successively deleting the root we get the elements

    sorted

    P f (D ) N G d