11. Heap Sort

download 11. Heap Sort

of 20

Transcript of 11. Heap Sort

  • 8/8/2019 11. Heap Sort

    1/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 11

    DATASTRUCTURES

    MAHESH GOYANI

    MAHATMAGANDHI INSTITUE OF TECHNICALEDUCATION & RESEARCH CENTER

    [email protected]

  • 8/8/2019 11. Heap Sort

    2/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 22

    HEAP SORT

  • 8/8/2019 11. Heap Sort

    3/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 33

    HEAP SORT

    A heap is defined as an almost complete binary tree of n nodes such that thevalue of each node is less than or equal to its father.

    A [j]

  • 8/8/2019 11. Heap Sort

    4/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 44

    33

    42

    33

    42

    33

    42

    67

    67

    42

    67

    2323

    44

    44

    33

    4449

    49

    42

    49

    7474

    49

    74

    67

    74

    Heap Is : 74, 44, 67, 23, 33, 42, 49

    HEAP CREATION Input Is : 33, 42, 67, 23, 44, 49, 74

  • 8/8/2019 11. Heap Sort

    5/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 55

    Input n Element in Heap H

    Add new node by incrementing size of heap H: n = n + 1 & LOC = n

    While (LOC < 1), Repeat step 4 to 7

    PAR = LOC / 2

    If (DATA < HA [PAR])

    HA [LOC] = DATA

    Exit

    HA [LOC] = HAP [PAR]

    HA [1] = DATA

    Exit

    INSERT NODE

  • 8/8/2019 11. Heap Sort

    6/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 66

    996040

    8020

    10

    50 700

    85

    65

    996040

    8020

    10

    50 700

    85

    65 15

    MIN HEAP

  • 8/8/2019 11. Heap Sort

    7/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 77

    996040

    8020

    10

    50 700

    85

    65 15

    992040

    8015

    10

    50 700

    85

    65 60

  • 8/8/2019 11. Heap Sort

    8/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 88

    16

    5 11

    3 18 16

    18 11

    3 5 18

    16 11

    3 5

    MAX HEAP

  • 8/8/2019 11. Heap Sort

    9/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 99

    996040

    8020

    10

    50 700

    85

    65

    DELETE ROOT

  • 8/8/2019 11. Heap Sort

    10/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1010

    996040

    8020

    10

    50 700

    85

    65

    996040

    8020

    65

    50 700

    85

    65

    DELETE ROOT

  • 8/8/2019 11. Heap Sort

    11/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1111

    996040

    8020

    65

    700 50

    85 996050

    8040

    20

    700 65

    85

    DELETE ROOT

  • 8/8/2019 11. Heap Sort

    12/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1212

    Forming the heap from an unsorted arrayForming the heap from an unsorted array

    11 26 14 2 19 32 7

    32

    214711

    1926

  • 8/8/2019 11. Heap Sort

    13/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1313

    Populating the new arrayPopulating the new array

    32

    214711

    1926

  • 8/8/2019 11. Heap Sort

    14/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1414

    Reforming the heapReforming the heap

    32

    19

    2711

    1426

  • 8/8/2019 11. Heap Sort

    15/20

  • 8/8/2019 11. Heap Sort

    16/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1616

    Repeat the processRepeat the process

    32 26

    2711

    1419

  • 8/8/2019 11. Heap Sort

    17/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1717

    32 26

    14

    711

    219

    Repeat the processRepeat the process

  • 8/8/2019 11. Heap Sort

    18/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1818

    32 26

    19

    711

    214

    Repeat the processRepeat the process

  • 8/8/2019 11. Heap Sort

    19/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 1919

    32 26 19

    711

    214

    Repeat the processRepeat the process

  • 8/8/2019 11. Heap Sort

    20/20

    (C) GOYANI MAHESH(C) GOYANI MAHESH 2020

    COMPLEXITY

    The heap sort is the slowest of the O(nlog n) sorting algorithms

    But unlike the merge and quick sorts it doesn't require massive recursion ormultiple arrays to work. This makes it the most attractive option for very largedata sets of millions of items.