• What Big O notation looks like What’s a good algorithm to draw this grid Drawing a grid one box at a time
  • Drawing a grid in four folds
  • Visualizing different Big O run times




    Download 24.82 Mb.
    Pdf ko'rish
    bet14/120
    Sana30.06.2022
    Hajmi24.82 Mb.
    #24606
    1   ...   10   11   12   13   14   15   16   17   ...   120
    Bog'liq
    grokking-algorithms-illustrated-programmers-curious
    ppt 1, Oraliq nazorat Javoblari, 5-mustaqil ish, 5-мустақил иш, Илмий тадқиқот методологияси ишчи дастур 2020, Allayarov A. 2, ТПроформа договора по лоту №73719 (1), 7-topshiriq, 7-8-9 lab, Amplitudali modulyatsiya, 1-1610, fazliddin fozilov 123, Dasturlash 3 natija, Calendar plan-Maxsus fanlarni o\'qitish metodikasi (3)
    Visualizing different Big O run times
    Here’s a practical example you can follow at 
    home with a few pieces of paper and a pencil. 
    Suppose you have to draw a grid of 16 boxes.
    Algorithm 1
    One way to do it is to draw 16 boxes, one at 
    a time. Remember, Big O notation counts 
    the number of operations. In this example, 
    drawing one box is one operation. You have
    to draw 16 boxes. How many operations will
    it take, drawing one box at a time?
    It takes 16 steps to draw 16 boxes. What’s the running time for this
    algorithm?
    What Big O
    notation looks like
    What’s a good 
    algorithm to
    draw this grid?
    Drawing a grid
    one box at a time


    Chapter 1 
     
    I
     
     
    Introduction to algorithms
    14
    Algorithm 2
    Try this algorithm instead. Fold the paper.
    In this example, folding the paper once is an operation. You just made 
    two boxes with that operation! 
    Fold the paper again, and again, and again.
    Unfold it after four folds, and you’ll have a beautiful grid! Every fold 
    doubles the number of boxes. You made 16 boxes with 4 operations!
    You can “draw” twice as many boxes with every fold, so you can draw 
    16 boxes in 4 steps. What’s the running time for this algorithm? Come 
    up with running times for both algorithms before moving on.
    Answers: Algorithm 1 takes O(n) time, and algorithm 2 takes
    O(log n) time.
    Drawing a grid
    in four folds


    15
    Big O notation
    Big O establishes a worst-case run time
    Suppose you’re using simple search to look for a person in the phone 
    book. You know that simple search takes O(n) time to run, which 
    means in the worst case, you’ll have to look through every single entry 
    in your phone book. In this case, you’re looking for Adit. This guy is 
    the first entry in your phone book. So you didn’t have to look at every 
    entry—you found it on the first try. Did this algorithm take O(n) time? 
    Or did it take O(1) time because you found the person on the first try?
    Simple search still takes O(n) time. In this case, you found what you 
    were looking for instantly. That’s the best-case scenario. But Big O 
    notation is about the worst-case scenario. So you can say that, in the 
    worst case, you’ll have to look at every entry in the phone book once. 
    That’s O(n) time. It’s a reassurance—you know that simple search will 
    never be slower than O(n) time.

    Download 24.82 Mb.
    1   ...   10   11   12   13   14   15   16   17   ...   120




    Download 24.82 Mb.
    Pdf ko'rish