Algorithms For Dummies


Arranging and Searching Data



Download 7,18 Mb.
Pdf ko'rish
bet252/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   248   249   250   251   252   253   254   255   ...   651
Bog'liq
Algorithms

  Arranging and Searching Data 

     139


    # Define variables used to merge the two pieces.

    result = []

    leftIndex = 0

    rightIndex = 0

    totalLen = len(left) 

+ len(right)

  

    # Keep working until all of the items are merged.



    while (len(result) < totalLen):

  

        # Perform the required comparisons and merge



        # the pieces according to value.

        if left[leftIndex] < right[rightIndex]:

            result.append(left[leftIndex])

            leftIndex

+= 1

        else:



            result.append(right[rightIndex])

            rightIndex

+= 1

  

        # When the left side or the right side is longer,



        # add the remaining elements to the result.

        if leftIndex == len(left) or \

            rightIndex == len(right):

                result.extend(left[leftIndex:]

                              or right[rightIndex:])

                break

  

    return result



  

mergeSort(data)

The 

print


 statements in the code help you see how the merging process works. 

Even though the process seems quite complex, it really is relatively straight-

forward when you work through the merging process shown here.

Left side:  [9]

Right side:  [5]

Merged  [5, 9]

Left side:  [4]

Right side:  [2]

Merged  [2, 4]

Left side:  [7]

Right side:  [2, 4]

Merged  [2, 4, 7]

Left side:  [5, 9]



140

 

   


  PART 2 


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   248   249   250   251   252   253   254   255   ...   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