Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet144/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   140   141   142   143   144   145   146   147   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Step

Value of Variable

Description

Code

1

list: [0, 6, 8, 14, 32]

sort the list immediately

list.sort( )

2

mid: 2

Find the middle, 5 / 2, round down

len(list) // 2

3

value: 8

don’t return true, 8 is not 14

list[2]

4

condition: False

8 is less than 14 don’t run block

if list[2] > 14

5

list: [14, 32]

Run block, cut off first half of list

list = list[mid + 1 : ]

2

mid: 1

Middle index is 1 because 2 / 2

len(list) // 2

3

value: 32

don’t return true, 32 is not 14

list[1]

4

list: [14]

Run block, cut off second half of list

list = list[ : mid - 1]

2

mid: 0

Find the middle, 1 / 2, round down

len(list) // 2

3

return true

value at mid index is 14 return true

return True

A linear search would require us to search the list item by item to see if the number we’re looking for was in the list. When thinking about efficiency and how long a search may take to complete the task, it would be based on the length of the list. As the length of the list grows, so does the time it takes to find the number we’re looking for. With a Binary Search, however, the time it takes to find a number within a list only takes a minimal number of steps even when the list is a million numbers. For example, when you search a list of one million numbers, a linear search could take one million tries to find the number, but a Binary Search would be able to find it within 20 guesses. As it searches, it cuts the list in half. Within 10 guesses you’re already working with a list of under 2,000 items. This is the beauty of an efficient algorithm. Let’s walk through each step together to understand how the algorithm is programmed.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   140   141   142   143   144   145   146   147   ...   200




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