Algorithms For Dummies


•  Choosing a random index •



Download 7,18 Mb.
Pdf ko'rish
bet255/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   251   252   253   254   255   256   257   258   ...   651
Bog'liq
Algorithms

• 

Choosing a random index



• 

Choosing the middle index of the partition



• 

Choosing the median of the first, middle, and last element of the partition for the 

pivot (especially for longer partitions)



142

 

   


  PART 2 

 Understanding the Need to Sort and Search

The inner loop of this example continuously searches for elements that are in the 

wrong place and swaps them. When the code can no longer swap items, it breaks 

out of the loop and sets a new pivot point, which it returns to the caller. This is the 

iterative part of the process. The recursive part of the process handles the left and 

right side of the dataset, as shown here:

def quickSort(data, left, right):

    if right <= left:

        return

    else:

        pivot = partition(data, left, right)

        quickSort(data, left, pivot-1)

        quickSort(data, pivot

+1, right)

    return data

  

quickSort(data, 0, len(data)-1)



The amount of comparisons and exchanges for this example are relatively small 

compared to the other examples. Here is the output from this example:

[9, 5, 7, 4, 2, 8, 1, 3, 6, 10]

[6, 5, 7, 4, 2, 8, 1, 3, 9, 10]

[6, 5, 3, 4, 2, 8, 1, 7, 9, 10]

[6, 5, 3, 4, 2, 1, 8, 7, 9, 10]

[1, 5, 3, 4, 2, 6, 8, 7, 9, 10]

[1, 5, 3, 4, 2, 6, 8, 7, 9, 10]

[1, 2, 3, 4, 5, 6, 8, 7, 9, 10]

[1, 2, 3, 4, 5, 6, 8, 7, 9, 10]

[1, 2, 3, 4, 5, 6, 8, 7, 9, 10]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]




Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   251   252   253   254   255   256   257   258   ...   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