Analysis of Heapsort

download Analysis of Heapsort

of 37

Transcript of Analysis of Heapsort

  • 8/19/2019 Analysis of Heapsort

    1/37

     Analysis of Algorithms

    CMSC 142

     Analysis of Heapsort

  • 8/19/2019 Analysis of Heapsort

    2/37

    2

  • 8/19/2019 Analysis of Heapsort

    3/37

    3FEW UNIQUE RANDOM REVERSED ALMOST SORTED

    MERGE SORT

    INSERTION SORT

    HEAP SORT

  • 8/19/2019 Analysis of Heapsort

    4/37

    4

    Heap Types

    • Max-heaps (largest element at root), have themax-heap property: 

      for all no!es i, e"#l$!ing the root%

    A[PARENT(i)] ≥ A[i]

    • Min-heaps (smallest element at root), have the

    min-heap property:  for all no!es i, e"#l$!ing the root%

    A[PARENT(i)] ≤ A[i]

  • 8/19/2019 Analysis of Heapsort

    5/37

    &

     A!!ing'eleting o!es

    • e* no!es are al*ays inserte! at the +ottomlevel (left to right)

    • o!es are remove! from the +ottom level (right

    to left)

  • 8/19/2019 Analysis of Heapsort

    6/37

    -perations on Heaps

    • Maintain'.estore the ma"/heap property  MA0/HA5

    • Create a ma"/heap from an $nor!ere! array

      678/MA0/HA

    • Sort an array in pla#e

      HAS-.T

    • riority 9$e$es

  • 8/19/2019 Analysis of Heapsort

    7/37

    :

    Maintaining the Heap roperty

    • S$ppose a no!e is smaller than a#hil!

      8eft an! .ight s$+trees of i are ma"/heaps

    • To eliminate the violation%

      "#hange *ith larger #hil!  Move !o*n the tree

      Contin$e $ntil no!e is not smaller than

    #hil!ren

  • 8/19/2019 Analysis of Heapsort

    8/37

    ;

    "ample

    MA0/HA5(A, 2, 1 violates the heap property

     A=2> ↔ A=4>

     A=4> violates the heap property

     A=4> ↔ A=?>

    Heap property restore!

  • 8/19/2019 Analysis of Heapsort

    9/37

    ?

    Maintaining the Heap roperty

    •  Ass$mptions%  8eft an! .ight

    s$+trees of i arema"/heaps

    – A[i] may +esmaller than its

    #hil!ren

    Alg: MA0/HA5(A, i, n)1. l @ 8T(i)

    2. r @ .HT(i)

    3. if   l ≤ n an! A[l] > A[i]

    4B then largest l←

    &B else largest i←

    6. if   r ≤ n an! A[r] > A[largest]

    :B then largest r

    8. if   largest ≠ i

    ?B then e"#hange A[i] A[largest]↔

    1

  • 8/19/2019 Analysis of Heapsort

    10/37

    1<

    MA0/HA5 .$nning Time

    • nt$itively%

    • .$nning time of MA0/HA5 is O(lgn) 

    • Can +e *ritten in terms of the height of the heap,

    as +eing O(h)

      Sin#e the height of the heap is lgn

    h

    2h-(h)

    ////

  • 8/19/2019 Analysis of Heapsort

    11/37

    11

    6$il!ing a Heap

    Alg: 678/MA0/HA(A)

    1. n  length=A>

    2B for  i ← n/2 downto 1

    3B do MA0/HA5(A, i, n)

    •Convert an array A[1 n] into a ma"/heap (n ! length[A])

    • The elements in the s$+array A[(n/2"1) .. n] are leaves

    •  Apply MA0/HA5 on elements +et*een 1 an! n/2

    2

    14 ;

    1

    1

    :

    4

    3

    ? 1<

    1

    2 3

    4 & :

    ; ? 1<

    4 1 3 2 1 ? 1< 14 ; : A%

  • 8/19/2019 Analysis of Heapsort

    12/37

  • 8/19/2019 Analysis of Heapsort

    13/37

    13

    .$nning Time of 678 MA0 HA

    ⇒ .$nning time% O(nlgn)

    • This is not an asymptoti#ally tight $pper +o$n!

    Alg: 678/MA0/HA(A)1. n  length=A>

    2B for  i ← n/2 downto 1

    3B do MA0/HA5(A, i, n) O(lgn)

    O(n)

  • 8/19/2019 Analysis of Heapsort

    14/37

    14

    .$nning Time of 678 MA0 HA

    • HA5 taDes O(h) ⇒ the #ost of HA5 on a no!e i isproportional to the height of the no!e i in the tree

    Height 8evel

    h

  • 8/19/2019 Analysis of Heapsort

    15/37

    1&

    .$nning Time of 678 MA0 HA

    i

    h

    i

    ihnnT 

    ∑==

    0

    )( Cost of HA5 at level i ∗ n$m+er of no!es at that level

    ( )ihh

    i

    i−=∑

    =0

    2 .epla#e the val$es of ni an! hi #omp$te! +efore

    h

    h

    i

    ih

    ih

    220∑= −−

    =

    M$ltiply +y 2h +oth at the nominator an! !enominator an!

    *rite 2i asi−2

    1

    ∑=

    =h

    h   k 

    0 22 Change varia+les% D h / i

    ∑∞

    =

    ≤0  2k 

    k n The s$m a+ove is smaller than the s$m of all elements to ∞

    an! h lgn

    )(nO= The s$m a+ove is smaller than 2

    .$nning time of 678/MA0/HA%T(n) ! O(n)

  • 8/19/2019 Analysis of Heapsort

    16/37

    1

    Heapsort

    • oal%

      Sort an array $sing heap representations

    • !ea%

     6$il! a max-heap from the array

      S*ap the root (the ma"im$m element) *ith the last

    element in the array

      Eis#ar!F this last no!e +y !e#reasing the heap siGe

      Call MA0/HA5 on the ne* root

      .epeat this pro#ess $ntil only one no!e remains

  • 8/19/2019 Analysis of Heapsort

    17/37

    1:

    "ample% A=:, 4, 3, 1, 2>

    MA0/HA5(A, 1, 4) MA0/HA5(A, 1, 3) MA0/HA5(A, 1, 2)

    MA0/HA5(A, 1, 1)

  • 8/19/2019 Analysis of Heapsort

    18/37

    1;

    Alg: HAS-.T(A)

    1B 678/MA0/HA(A)

    2B for i length[A]←  downto 2

    3B do e"#hange A[1] A[i]↔

    4B MA0/HA5(A, 1, i # 1)

    • .$nning time% O(nlgn) ### $an %e

    sh&'n t& %e (nlgn)

    O(n)

    O(lgn)

    n#1 times

    - i

  • 8/19/2019 Analysis of Heapsort

    19/37

    1?

    -perations

    on riority $e$es

    • Ma"/priority 9$e$es s$pport the follo*ingoperations%

      S.T(, *)% inserts element * into set

      0T.ACT/MA0()% removes an! ret$rns element of

     *ith largest Dey

      MA0M7M()% ret$rns element of  *ith largest Dey

      C.AS/I5(, *, +)% in#reases val$e of element

    *Js Dey to + (Ass$me + ≥ *Js #$rrent Dey val$e)

  • 8/19/2019 Analysis of Heapsort

    20/37

    2<

    HA/MA0M7M

    oal%  .et$rn the largest element of the heap

    Alg: HA/MA0M7M(A)1B return A[1]

     .$nning time% O(1)

    Heap A%

    Heap/Ma"im$m(A) ret$rns :

  • 8/19/2019 Analysis of Heapsort

    21/37

    21

    HA/0T.ACT/MA0oal%

      "tra#t the largest element of the heap (iBeB, ret$rn the ma"

    val$e an! also remove that element from the heap

    !ea%

      "#hange the root element *ith the last

      e#rease the siGe of the heap +y 1 element

      Call MA0/HA5 on the ne* root, on a heap of siGe n/1

    Heap A% .oot is the largest element

  • 8/19/2019 Analysis of Heapsort

    22/37

    22

    "ample% HA/0T.ACT/MA0

    ;

    2 4

    14

    :

    1

    1

    1<

    ? 3

    a* ! 1-;

    2 4

    14

    :

    1

    1<

    ? 3

    Heap siGe !e#rease! *ith 1

    4

    2 1

    ;

    :

    14

    1<

    ? 3

    Call MA0/HA5(A, 1, n#1)

  • 8/19/2019 Analysis of Heapsort

    23/37

    23

    HA/0T.ACT/MA0

    Alg: HA/0T.ACT/MA0(A, n)1B if n 1

    2B then error Eheap $n!erflo*F

    3B a* A[1]←

    4B A[1] A[n]←

    &B MA0/HA5(A, 1, n#1)  remaDes heapB return a*

     .$nning time% O(lgn)

  • 8/19/2019 Analysis of Heapsort

    24/37

    24

    HA/C.AS/I5

    • oal%

      n#reases the Dey of an element i in the heap

    • !ea%

      n#rement the Dey of A[i] to its ne* val$e

      f the ma"/heap property !oes not hol! anymore%traverse a path to*ar! the root to fin! the proper

    pla#e for the ne*ly in#rease! Dey

    ;

    2 4

    14

    :

    1

    1

    1<

    ? 3i

    e0 [i] @ 1&

  • 8/19/2019 Analysis of Heapsort

    25/37

    2&

    "ample% HA/C.AS/I5

    14

    2 ;

    1&

    :

    1

    1

    1<

    ? 3

    i

    ;

    2 4

    14

    :

    1

    1

    1<

    ? 3i

    Key =i > @ 1&

    ;

    2 1&

    14

    :

    1

    1

    1<

    ? 3i

    1&

    2 ;

    14

    :

    1

    1

    1<

    ? 3i

  • 8/19/2019 Analysis of Heapsort

    26/37

    2

    HA/C.AS/I5

    Alg: HA/C.AS/I5(A, i, +e0)

    1B if +e0 A[i]

    2B then error Ene* Dey is smaller than #$rrent DeyF

    3B A[i] +e0←

    4B while i > 1 an! A[PARENT(i)] A[i]

    &B do e"#hange A[i] A[PARENT(i)]↔

    B i PARENT(i)←

    • .$nning time% O(lgn)

    ;

    2 4

    14

    :

    1

    1

    1<

    ? 3i

    e0 [i] @ 1&

  • 8/19/2019 Analysis of Heapsort

    27/37

    2:

    /∞

    MA0/HA/S.T

    • oal%  nserts a ne* element into a ma"/

    heap

    • !ea%

      "pan! the ma"/heap *ith a ne*

    element *hose Dey is /∞

      Calls HA/C.AS/I5 to

    set the Dey of the ne* no!e to its

    #orre#t val$e an! maintain the

    ma"/heap property

    ;

    2 4

    14

    :

    1

    1

    1<

    ? 3

    1&

    ;

    2 4

    14

    :

    1

    1

    1<

    ? 3

  • 8/19/2019 Analysis of Heapsort

    28/37

    2;

    "ample% MA0/HA/S.T

    /∞;

    2 4

    14

    :1

    1

    1<

    ? 3

    nsert val$e 1&%

    / Start +y inserting /∞

    1&;

    2 4

    14

    :1

    1

    1<

    ? 3

    n#rease the Dey to 1&

    Call HA/C.AS/I5 on A=11> 1&

    :

    ;

    2 4

    14

    1&

    1

    1

    1<

    ? 3

    :

    ;

    2 4

    1&

    14

    1

    1

    1<

    ? 3

    The restore! heap #ontaining

    the ne*ly a!!e! element

  • 8/19/2019 Analysis of Heapsort

    29/37

    2?

    MA0/HA/S.T

    Alg: MA0/HA/S.T(A, +e0, n)

    1B hea#sie[A] @ n " 1

    2B A[n " 1] #←   ∞ 

    3B HA/C.AS/I5(A, n " 1, +e0)

     .$nning time% O(lgn)

    /∞

    ;

    2 4

    14

    :

    1

    1

    1<

    ? 3

  • 8/19/2019 Analysis of Heapsort

    30/37

    3<

    S$mmary

    • Ke #an perform the follo*ing operations onheaps%

      MA0/HA5 O(lgn)

      678/MA0/HA O(n)  HA/S-.T O(nlgn)

      MA0/HA/S.T O(lgn)

      HA/0T.ACT/MA0 O(lgn)  HA/C.AS/I5 O(lgn)

      HA/MA0M7M O(1)

    A3erage  O(lgn)

  • 8/19/2019 Analysis of Heapsort

    31/37

    HeapSort 8oop nvariant

    31

  • 8/19/2019 Analysis of Heapsort

    32/37

  • 8/19/2019 Analysis of Heapsort

    33/37

    8-- LA.AT

    33

  • 8/19/2019 Analysis of Heapsort

    34/37

    34

    Alg: 678/MA0/HA(A)

    1. n  length=A>2B for  i ← n/2 downto 1

    3B do MA0/HA5(A, i, n)

  • 8/19/2019 Analysis of Heapsort

    35/37

  • 8/19/2019 Analysis of Heapsort

    36/37

    8esson from a HeapSort

    3

     Ang +$hay ay parang heapsort

    Merong mas ma+a+a saJyo

    Meron !in namang nasa itaas mo

    Mas

    magaling

    aDo

    Hi there

  • 8/19/2019 Analysis of Heapsort

    37/37

    3:

    "ample% A=:, 4, 3, 1, 2>

    MA0/HA5(A, 1, 4) MA0/HA5(A, 1, 3) MA0/HA5(A, 1, 2)

    MA0/HA5(A, 1, 1)

    ero tan!aan mo, !arating !in ang panahonN

    Jm on the

    top of the

    *orl!

    D t Dt D it