Grokking Algorithms



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

Chapter 1
 
 
I
 
 
Introduction to algorithms
12
No. Turns out, Bob is wrong. Dead wrong. The run time for simple 
search with 1 billion items will be 1 billion ms, which is 11 days! The 
problem is, the run times for binary search and simple search 
don’t 
grow at the same rate
.
That is, as the number of items increases, binary search takes a little 
more time to run. But simple search takes a
 lot
more time to run. So 
as the list of numbers gets bigger, binary search suddenly becomes a 
lot 
faster than simple search. Bob thought binary search was 15 times 
faster than simple search, but that’s not correct. If the list has 1 billion 
items, it’s more like 33 million times faster. That’s why it’s not enough 
to know how long an algorithm takes to run—you need to know how 
the running time increases as the list size increases. That’s where Big O 
notation comes in.
Big O notation tells you how fast an algorithm is. For example, suppose 
you have a list of size 
n
. Simple search needs to check each element, so 
it will take 
n
operations. The run time in Big O notation is O(
n
). Where 
are the seconds? There are none—Big O doesn’t tell you the speed in 
seconds. 
Big O notation lets you compare the number of operations.
It 
tells you how fast the algorithm grows.
Run times grow at
very different speeds!


13
Big O notation
Here’s another example. Binary search needs log 
n
operations to check 
a list of size 
n
. What’s the running time in Big O notation? It’s O(log 
n
). 
In general, Big O notation is written as follows.
This tells you the number of operations an algorithm will make. It’s 
called Big O notation because you put a “big O” in front of the number 
of operations (it sounds like a joke, but it’s true!).
Now let’s look at some examples. See if you can figure out the run time 
for these algorithms.

Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   9   10   11   12   13   14   15   16   ...   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