AVERAGE CASE 66-70

download AVERAGE CASE 66-70

of 24

Transcript of AVERAGE CASE 66-70

  • 8/8/2019 AVERAGE CASE 66-70

    1/24

    1

    AVERAGE CASE ANALYSIS OF

    QUICK SORT

    CA(n)= n+1+1/n CA(k-1)+CA(n-k)-----(1)

    1eken

    (n+1) is the number of element comparisons required byPARTITION on its first call

    (@PARTITION (1, n+1) (n=5, say) with A(n+1)=infinitywith all elements in decreasing order stops with i6 and

    p

    5 and i>5 so total comparisons are 5+1 =6

    for the listv = 75 65 55 45 35 + E

    1 2 3 4 p5 i6

  • 8/8/2019 AVERAGE CASE 66-70

    2/24

    2

    AVERAGE CASE ANALYSIS OF

    QUICK SORT (Contd..) After the first call, one element is sorted and remaining n-1

    elements are divided into two sets may be consisting of (0, n-1) or

    (1, n-2) or or (n-1, 0) i.e. in general (k-1, n-k) elements 1e

    ke

    nclearly CA(0)=CA(1)=0

    Multiplying both sides of (1) by n,

    nCA (n)=n(n+1)+2(CA(0)+CA(1)+------+CA(n-1)--------(2)

    (@ CA(k-1)+CA(n-k)=CA(0)+CA(n-1)+CA(1)+CA(n-2)+.+CA(n-1)+CA(0)1eken

    =2(CA(0)+CA(1)+.+CA(n-1))

  • 8/8/2019 AVERAGE CASE 66-70

    3/24

    3

    AVERAGE CASE ANALYSIS OF

    QUICK SORT (Contd..)

    Replacing n by n-1 in (2),

    (n-1)CA(n-1)=n(n-1)+2(CA(0)+.+CA(n-2))..(3)

    (2) - (3) gives

    nCA(n)-(n-1)CA(n-1)=2n+2CA(n-1)

    @nCA(n)=2n+(n+1)CA(n-1)(4)

    CA(n)/(n+1)=CA(n-1)/n + 2/(n+1) {dividing (4) byn(n+1)

  • 8/8/2019 AVERAGE CASE 66-70

    4/24

    4

    AVERAGE CASE ANALYSIS OF

    QUICK SORT (Contd..)

    CA(n)/ (n+1)=CA(n-1)/n +2/(n+1).(5)

    Substituting n-1 instead of n in the above equation

    CA(n-1)/n=CA(n-2)/ (n-1) + 2/n(6)

    Substituting (6) in (5)

    CA(n)/(n+1)=CA(n-2)/(n-1) +2/n +2/(n+1)

    =CA(n-3)/(n-2) +2/(n-1) +2/n + 2/(n+1) .

    =CA(1)/2+ 2 (1/k) =2 (1/k)

    3eken+1 3eken+1

    5 (CA(1) = 0)

  • 8/8/2019 AVERAGE CASE 66-70

    5/24

    5

    AVERAGE CASE ANALYSIS OF

    QUICK SORT (Contd..)

    1/ke 2n+1 1/x dx = loge

    n+1-loge2

    1eken+1

    @ CA(n)

  • 8/8/2019 AVERAGE CASE 66-70

    6/24

    6

    Iterative version of Quick Sort

    Procedure : quick sort2(p,q)

    Integer STACK (1:max), top

    gobal A(1:n); local integer i

    Integer p,qinteger p,q

    top0

    loop

    while p

  • 8/8/2019 AVERAGE CASE 66-70

    7/24

    7

    Iterative version of Quick Sort(Contd..)if j-p < q-j then

    stack (top+1)j+1

    stack (top+2)q

    qj-1

    // store in stack the bands of larger file //

    else

    stack (top+1)p

    stack (top+2)j-1pj+1

    Endif

    toptop+2

    repeat

  • 8/8/2019 AVERAGE CASE 66-70

    8/24

    8

    Iterative version of Quick Sort(Contd..)

    if top=0 then return

    endif

    qstack (top); pstack(top-1)toptop-2

    repeat

    end quicksort2

  • 8/8/2019 AVERAGE CASE 66-70

    9/24

    9

    Iterative version of Quick Sort Example

    Example:

    65,70,75,89,85,60,55,50,45, infinity

    p = 1, q = 9, j10 partition(1,10)

    j 5 j p not q - j

    pj + 1 =

    6< q =

    941

  • 8/8/2019 AVERAGE CASE 66-70

    10/24

    10

    Iterative version of Quick Sort Example

    so partition (6,10) gives value j 9

    j p = 9-6 =3 not< q j = 9-9 = 0

    stack(3)6

    stack(4)8 8

    p j+1=10 q9 6

    and 6,8 are removed from stack 4

    10 not < 9 so while is exited 1

    (coming out of loop when top=0)

    means there are no elements to

    be sorted.

  • 8/8/2019 AVERAGE CASE 66-70

    11/24

    11

    The maximum stack space in iterative quick sort

    The maximum stack space in iterative quick sort is

    0(logn)

    Proof : Let s(n) be the maximum stack space neededS(n) e 2 + s -(n-1/2) 5 2 for p,q and s -(n-1)/2is the stack space for processing the remaining

    -(n-1)/2 elements

    e 2+2 + S(-(n-1)/22

    )e 2+2 +2+ S(- n-1/23)e 2+.......+2 (log2n times) +S (-n-1/2

    log2n)

  • 8/8/2019 AVERAGE CASE 66-70

    12/24

    12

    The maximum stack space in iterative quick sort

    (Contd..)

    e 2log2n +S(-n-1/n , but S-n-1/n= 0

    e 2log2n=2log

    e

    n/loge

    2 5 loge

    n=log2

    n.loge

    2

    @ log2n=loge

    n/loge2

    @ S(n) = O(logn)

    (log n=logen

    and logn

    2= logn

    2)Quick sort is faster than merge sort for

    average cases.

  • 8/8/2019 AVERAGE CASE 66-70

    13/24

  • 8/8/2019 AVERAGE CASE 66-70

    14/24

    14

    SELECTION PROBLEM (Contd..)

    integer n, k, m, r, j

    m1; rn+1; A(n+1)infinity

    loop

    jr

    call partition(m,j)

    case

    : k = j : return

    :k

  • 8/8/2019 AVERAGE CASE 66-70

    15/24

    15

    SELECTION PROBLEM

    (Contd..)

    Procedure selection sort (A,n)

    // sorts A(1)..A(n) using selection sort //

    for k1 to n do

    select (A,n,k)

    B(k)A(k)

    Repeat

    end select sort

  • 8/8/2019 AVERAGE CASE 66-70

    16/24

    16

    ANALYSIS OF SELECTAssumptions

    1.The n elements are distinct.

    2. Each element in A(m:p) has an equal probability

    of being the partitioning elements.

    Partition requires 0(m-p) time

    On each successive call to partitions m isincreased at least by 1 or p is decreased by at least

    one.

  • 8/8/2019 AVERAGE CASE 66-70

    17/24

    17

    ANALYSIS OF SELECT (Contd..)

    Hence at most n calls to Partitions may be made.

    Thus the worst case complexity of SELECT is

    1+2 +.+ n = n(n+1)/2 = O(n2

    )(@First call to partition has n elements

    2nd call to partitions has n-1 elements

    :

    nth call to partitions has 1 element.

  • 8/8/2019 AVERAGE CASE 66-70

    18/24

    18

    Average computing time of select (Contd..)

    Average computing time of select is O(n)

    Proof: Let TAk(n) be the average time to find the kth

    smallest element in A (1:n), taken over all n!different permutations of n distinct elements.

    Define TA(n) and R(n) as TA(n) = 1/n TAk(n)

    and R(n) = max k{ TAk(n) } 1 e ken

    TA(n) e R(n)

    TAk(n) e cn + 1/n ( TAk-i(n-i)+ TAi(i-1))1ei

  • 8/8/2019 AVERAGE CASE 66-70

    19/24

    19

    Average computing time of select (Contd..)

    cn is the time required by first call of partition, the partitionelement being the kth smallest with probability 1/n.

    After partitioning you get kth smallest, k being any one 1..n.

    Time for k-1th smallest of n-1

    (k-2)n-1 smallest of (n-2)

    1st smallest of (n-k+1) elements

    Time for (k+1)1st smallest of k elements

    (k+2)nd smallest of (k+1) elements

    :

    nth smallest of (n-1) elements

  • 8/8/2019 AVERAGE CASE 66-70

    20/24

    20

    Average computing time of select (Contd..)

    Considering maximum on both sides

    R(n) e cn + 1/n max { R(n-i) + R(i-1) ]---------(1)

    k 1ei< k k < ie n

    n-1 n-1

    = cn + 1/n max { R(i) + R(i) } n u 2

    k n-k+1 k

    (By changing the index from n-i to i)

  • 8/8/2019 AVERAGE CASE 66-70

    21/24

    21

    Average computing time of select (Contd..)

    We assume that c is chosen such that R(1) e

    c and

    we show by induction that R(n) e 4cn

    Base : n=25n u2 onwards the relation (1) is valid.

    R(n) e 2C + 1/2 max { R(1), R(1)}

    e 2C+ 1/2 CR = (5/2) C = 2.5C < 4cn

  • 8/8/2019 AVERAGE CASE 66-70

    22/24

    22

    Average computing time of select (Contd..)

    InductionHypothesis: Assume R(n) e 4cn for alln, 2 e n < m ------ (2)

    Induction step: For n = m,

    m-1 m-1

    R(m) e cm + 1/m max { R(i) + R(i)} --- (3)k m-k+1 k

    R(n) = max {TKA(n) } is a non-decreasing function ofn. k

  • 8/8/2019 AVERAGE CASE 66-70

    23/24

    23

    Average computing time of select (Contd..)

    m-1 m-1

    So R(i) + R(i) is maximized if k = m/2m-k+1 k

    when m is even, and k = (m+1)/2 when m is odd.

    Because

    m-1 m-1 m-1 m-1max { R(i) + R(i)} = max { R(i) + R(i)},

    k m-k+1 k (k=1) i=m i=1

  • 8/8/2019 AVERAGE CASE 66-70

    24/24

    24

    Average computing time of select (Contd..)

    m-1 m-1 m-1 m-1 m-1 m-1

    ( R(i) + R(i)) ( R(i) + R(i))..., ( R(i) R(i)) }(K=2) i=m-1 i=2

    (

    k=m/2) i=m/2+1 i=m/2 (k=m-1) i=2 i=m-1

    Thus if m is even, m-1 m-1 m-1R(m) e cm + 2/m R(i) 5 R(i) e R(i)

    m/2 m/2+1 m/2

    e cm + 2/m [R(m/2+R(m/2+1)++R(m-1)]

    e cm + 2/m [4cm/2+4c(m/2+1)++4c(m-1)]

    m-1

    e cm + 8c/m i < 4cmm/2