Algorithms For Dummies


Using Randomized Algorithms



Download 7,18 Mb.
Pdf ko'rish
bet540/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   536   537   538   539   540   541   542   543   ...   651
Bog'liq
Algorithms

  Using Randomized Algorithms 

     333


    return quickselect(right, k)

quickselect(series, 250)

14.0

The algorithm works well because it keeps reducing the problem size. It works 



best when the random pivot number is drawn nearer to the kth position (the stop-

ping rule is that the pivot number is the value in the kth position). Unfortunately, 

because you can’t know the kth position in the unordered list, drawing randomly 

by using a uniform distribution (each element in the list has the same chance of 

being chosen) is the best solution because the algorithm eventually finds the right 

solution. Even when random chance doesn’t work in the algorithm’s favor, the 

algorithm keeps on reducing the problem, thus getting more chances to find the 

solution, as demonstrated earlier in the chapter when guessing the cards ran-

domly  picked  from  a  deck.  As  the  deck  gets  smaller,  guessing  the  answer  gets 

easier. The following code shows how to use Quickselect to determine the median 

of a list of numbers:

def median(series):

    if len(series) % 2 != 0:

        return quickselect(series, len(series)//2)

    else:

        left  = quickselect(series, (len(series)-1) // 2)

        right = quickselect(series, (len(series)

+1) // 2)

        return (left 

+ right) / 2

median(series)

14.0



Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   536   537   538   539   540   541   542   543   ...   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