Algorithms For Dummies


PART 1   Getting Started



Download 7,18 Mb.
Pdf ko'rish
bet214/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   210   211   212   213   214   215   216   217   ...   651
Bog'liq
Algorithms

 

   


  PART 1 

 Getting Started

Note that this example makes one of two recursive calls. When 

key

 is greater than 



the midpoint value of the existing list, 

searchList[mid]

, the code calls 

search


 

again with just the right side of the remaining list. In other words, every call to 

search

 uses just half the list found in the previous call. When 



key

 is less than or 

equal to 

searchList[mid]

, search receives the left half of the existing list.

The list may not contain a search value, so you must always provide an escape 

method for the recursion or the stack will fill, resulting in an error message. In 

this case, the escape occurs when 

mid == 0

, which means that there is no more 

searchList

  to  search.  For  example,  if  you  change 

search(aList, 5)

  to 


search(aList, 22)

, you obtain the following output instead:

Searching midpoint at  11

searchList now contains  [11, 12, 13, 14, 15, 16, 17, 18,

 19, 20]

Searching midpoint at  16

searchList now contains  [16, 17, 18, 19, 20]

Searching midpoint at  18

searchList now contains  [18, 19, 20]

Searching midpoint at  19

searchList now contains  [19, 20]

Searching midpoint at  20

searchList now contains  [20]

Searching midpoint at  20

Key Not Found!

Note also that the code looks for the escape condition before performing any other 

work to ensure that the code doesn’t inadvertently cause an error because of the 

lack of 


searchList

 content. When working with recursion, you must remain pro-

active or endure the consequences later.


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   210   211   212   213   214   215   216   217   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
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