Grokking Algorithms


Visualizing different Big O run times



Yüklə 348,95 Kb.
Pdf görüntüsü
səhifə14/122
tarix05.12.2023
ölçüsü348,95 Kb.
#173611
1   ...   10   11   12   13   14   15   16   17   ...   122
grokking-algorithms-illustrated-programmers-curious

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.

Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   10   11   12   13   14   15   16   17   ...   122




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin