Grokking Algorithms



Download 6,4 Mb.
Pdf ko'rish
bet8/120
Sana21.12.2022
Hajmi6,4 Mb.
#893167
1   ...   4   5   6   7   8   9   10   11   ...   120
Bog'liq
Grokking Algorithms An Illustrated Guide for Programmers and Other

Chapter 1
 
 
I
 
 
Introduction to algorithms
4
For example:
Here’s an example of how binary search works. I’m thinking of a 
number between 1 and 100. 
You have to try to guess my number in the fewest tries possible. With 
every guess, I’ll tell you if your guess is too low, too high, or correct.
Suppose you start guessing like this: 1, 2, 3, 4 …. Here’s how it would 
go.
Looking for companies 
in a phone book with 
binary search


5
Binary search
his is
simple search
(maybe 
stupid search
would be a better term). With 
each guess, you’re eliminating only one number. If my number was 99, 
it could take you 99 guesses to get there!
A better way to search
Here’s a better technique. Start with 50.
Too low, but you just eliminated 
half
the numbers! Now you know that 
1–50 are all too low. Next guess: 75. 
A bad approach to 
number guessing


Chapter 1
 
 
I
 
 
Introduction to algorithms
6
Too high, but again you cut down half the remaining numbers! 
With 
binary search, you guess the middle number and eliminate half the 
remaining numbers every time
. Next is 63 (halfway between 50 and 75).
his is binary search. You just learned your irst algorithm! Here’s how 
many numbers you can eliminate every time.
Whatever number I’m thinking of, you can guess in a maximum of 
seven guesses—because you eliminate so many numbers with every 
guess!
Suppose you’re looking for a word in the dictionary. he dictionary has 
240,000 words. 
In the worst case
, how many steps do you think each 
search will take?
Simple search could take 240,000 steps if the word you’re looking for is 
the very last one in the book. With each step of binary search, you cut 
the number of words in half until you’re let with only one word.

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   4   5   6   7   8   9   10   11   ...   120




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish