lec38-nov08

Upload: mukesh-kumar-dewra

Post on 05-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 lec38-nov08

    1/13

    1

    ESc101: Fundamentals of Com utin

    2011-12-Monsoon Semester

    Lecture #38, November 8, 2011

    Please switch off your mobile phones.

    Announcements

    Lab exam in the week of 14th to 18th November

    End-sem exam is on 25th November, 8:00 AM

    Copies can be seen on 28th afternoon.

    Lec-38 1Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

  • 7/31/2019 lec38-nov08

    2/13

    2

    Best Programmers of Lab 10

    Section Name Section Name

    E1 none E2 Harshad Sawhney

    E3 Anyesha Ghosh E4 Rajat Goel

    E5 Pratik Pradyot Rath E6 Raghav Goyal

    E7 Shivanshu Agrawal E8 Nikunj Agrawal

    E9 G V S Sasank Mouli E10 Prithvi Sharma

    E11 Ankush Sachdeva E12 Samyak Daga

    E13 Harsh Gupta E14 Amit Munje

    E15 none

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    2

    Recap

    Selection Sort

    Counting routes in a grid

    Number of bit-sum primes

    Next Palindrome

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    3

  • 7/31/2019 lec38-nov08

    3/13

    3

    Analyzing the efficiency of algorithms

    Sequential Search and Binary Search

    GCD_fast and GCD_slow

    Merge Sort and Selection Sort

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    4

    Searching

    Linear Search

    Start looking at all elements one by one

    When you find the element you were looking for, Stop.

    If you dont find the element, you will stop after looking at all the

    elements.

    Binary Search

    Look at the middle element. If this is the element you were looking

    for, Stop.

    If your element is smaller then the middle element, then your

    search s ace is the left half.

    If your element is bigger than the middle element, then your search

    space is the right half.

    Recursively apply the same algorithm to the reduced search space.

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    5

  • 7/31/2019 lec38-nov08

    4/13

    4

    Comparing Searching Algorithms

    It appears that sequential search might be slower, since it is

    checking all elements, while binary search is leaving out

    almost half the nodes in every iteration

    Can we be more formal about it?

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    6

    GCD

    // Assume m >= n int GCD slow int m, int n

    int GCD_fast (int m, int n)

    { int rem

    while (n!= 0){

    rem = m % n;

    m = n;

    _

    {

    while (n!= 0)

    { diff = m n;if (diff >= n) m = diff;

    else { m = n;

    n = diff;

    n = rem;

    }

    return m;

    }

    }

    }

    return m;

    }

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    7

  • 7/31/2019 lec38-nov08

    5/13

    5

    Comparing GCD algorithms

    may be faster than the algorithm based on subtraction.

    because it is reducing the numbers faster.

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    8

    Sorting

    To sort an array, divide the array into two parts, sort each

    part recursively, and then merge the two sorted sub-arrays to

    get the sorted array. Selection Sort:

    Find the minimum value in the array, swap it with the first

    osition and re eat these ste s for the remainder of the

    array, starting at the second position and advancing each

    time.

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    9

  • 7/31/2019 lec38-nov08

    6/13

    6

    Comparing Sorting Algorithms

    ,

    may find merge sort completing the sort earlier than

    selection sort.

    It may not be obvious, but it appears that in selection sort

    many comparisons are being repeated in successive

    iterations, while in merge sort no two numbers are

    com ared twice.

    This may be the reason for faster operation.

    Can we do this comparison more formally?

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    10

    What is the reason for different running times?

    ,

    GCD, Sorting):

    Have same input and output

    Are executed in same environment (PC, Operating system,Compiler)

    e nee o ana yze e num er o s eps ns ruc ons a en

    by each algorithm for a problem

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    11

  • 7/31/2019 lec38-nov08

    7/13

    7

    analyze its running time

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    12

    Number of steps?

    following loop:

    for (int i = 1; i

  • 7/31/2019 lec38-nov08

    8/13

    8

    Number of steps?

    following loop:

    for (int i = 1; i

  • 7/31/2019 lec38-nov08

    9/13

    9

    Number of steps?

    How many steps/instructions are executed in the following

    loop:

    for (int n = 1; n

  • 7/31/2019 lec38-nov08

    10/13

    10

    Number of steps?

    How many steps/instructions are executed in the following

    loop:

    for (int n = 1; n

  • 7/31/2019 lec38-nov08

    11/13

    11

    Sequential Search onn numbers

    ,

    iteratesn times in the worst case.

    In each iteration, we execute constant number of

    instructions.

    No. of instructions in the worst case:

    cn + d, for some constants c, d

    (for large values ofn, we can ignore d, hence, say,cn )

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    20

    Binary Search onn numbers

    int bin_search (int a[ ], int s, int left, int right)

    { int mid;

    while (left < right) {

    mid = (left + right)/2;

    if (a [mid] == s)

    return mid;

    else if (a [mid] > s)

    right = mid 1;

    e se eft = mi + 1;

    }

    return -1;

    }

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    21

  • 7/31/2019 lec38-nov08

    12/13

    12

    Analysis of Binary Search

    constant number of instructions in the worst case

    After each iteration of the while loop,

    search domain reduces by a factor of 2

    hence, the number of iterations of loop: log n

    Hence the number of instructions in the worst case:a log n + b, for some constants a, b

    (again, for large n, we can ignore b, hencea log n)

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    22

    Comparison of Search Algorithms

    Irrespective of the values ofb and c, for large enough

    values ofn, cn will be larger.

    Hence, we can say that sequential search takes more time

    compared to binary search.

    Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    23

  • 7/31/2019 lec38-nov08

    13/13

    13

    Any Questions?

    Lec-38 24Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon