lec40-nov15

Upload: mukesh-kumar-dewra

Post on 05-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 lec40-nov15

    1/7

  • 7/31/2019 lec40-nov15

    2/7

    2

    Recap

    GCD based on remainder and subtraction Merge sort and selection sort

    GCD Algorithm based on remainder function takes c log n steps Algorithm based on subtraction takes d n steps

    Sorting Selection sort takes c * n * n steps Merge sort takes d * n * log n steps

    Lec-40 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon

    2

    Recap: Selection Sort

    SelectionSort int a int n {

    for (int i = 0; i < n; i++)

    {int j = find index of smallest value between i and (n-1);swap values at i and j;

    }

    Loop is executed n times Finding smallest value takes c (n-i) + d instructions.Lec-40 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon3

  • 7/31/2019 lec40-nov15

    3/7

  • 7/31/2019 lec40-nov15

    4/7

    4

    Recap: Merge Sort

    . There are log n levels or numbers of mergers.

    Hence the worst case number of instructions are:

    c n log n

    Lec-40 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon

    6

    Comparison of Selection Sort and Merge Sort

    * ^ * *

    For any values of c1 and c2 , there is a value of n beyondwhich, c2 * n * log n will be smaller than c1 * n^2 .

    Hence merge sort is considered faster.

    Lec-40 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon

    7

  • 7/31/2019 lec40-nov15

    5/7

    5

    Alternate Analysis of Merge Sort

    - sort on n numbers. T(n) = d, if n = 1 T(n) = c * n + 2T (n/2), otherwise c * n is the number of instructions for merging two arrays. This is known as Recurrence Relation .

    Lec-40 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon

    8

    Alternate Analysis of Merge Sort

    Gradually unfold the recurrence:T(n) = cn + 2 (cn/2 + 2 T (n/4))

    = cn + cn + 4 T (n/4)

    = cn + cn + cn + 8 T (n/8)if n = 2^k, k = log n (base 2)

    T (n) = cn + cn + + cn (k terms) + 2^k T (n/2^k)= c n lo n + n T 1 = c n log n + n * d

    For large values of n, ignore n * d T(n) = c * n * log n

    Lec-40 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon

    9

  • 7/31/2019 lec40-nov15

    6/7

    6

    Analysis of exponential function

    ^ . If implemented by a single for loop with multiplication by

    2 in every iteration, then c*n . If implemented smartly as below, then c*log n

    pow (2, n) = 1, if n = 0pow (2, n) = pow (2, n/2) * pow (2, n/2), if n is even

    pow (2, n) = pow (2, (n-1)/2) * pow (2, (n-1)/2) * 2, if n is odd

    Lec-40 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon

    10

    Analysis of Counting Sort

    ,constant number of operations increase the frequency count of the that number in another

    array. Similarly, we do constant number of operations in printing

    each number

    Hence, number of instructions executed: c*n assuming n to be the numbers to be read, sorted, and

    printed

    Lec-40 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon

    11

  • 7/31/2019 lec40-nov15

    7/7

    7

    Analysis of Matrix Multiplication

    m matrix with a m x r matrix.

    The resultant matrix is an n x r matrix. For computing each element of this result matrix, we need to

    multiply m elements of first matrix (a row) withcorresponding elements of second matrix (a column)

    so, a t ese m num ers to e a e . Hence number of instructions for 1 element of result is c*m Total number of instructions: c*m*n*r

    Lec-40 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon

    12

    Any Questions?

    Lec-40 13Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon