Algorithms For Dummies


Employing better sort techniques



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

Employing better sort techniques

As sort technology improves, the sort algorithms begin taking a more intelligent 

approach to getting data into the right order. The idea is to make the problem 

smaller and easier to manage. Rather than work with an entire dataset

smart  sorting algorithms work with individual items, reducing the work required 

to perform the task. The following sections discuss two such smart sorting 

techniques.



138

 

   


  PART 2 

 Understanding the Need to Sort and Search

Rearranging data with Mergesort

A Mergesort works by applying the divide and conquer approach. The sort begins 

by breaking the dataset into individual pieces and sorting the pieces. It then 

merges the pieces in a manner that ensures that it has sorted the merged piece. 

The sorting and merging continues until the entire dataset is again a single piece. 

The worst-case sort speed of the Mergesort is O(n log n), which makes it consid-

erably faster than the techniques used in the previous section (because log n is 

always smaller than n). This sort actually requires the use of two functions. The 

first  function  works  recursively  to  split  the  pieces  apart  and  put  them  back 

together again.

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

  

def mergeSort(list):



    # Determine whether the list is broken into

    # individual pieces.

    if len(list) < 2:

        return list

  

    # Find the middle of the list.



    middle = len(list)//2

    # Break the list into two pieces.

    left = mergeSort(list[:middle])

    right = mergeSort(list[middle:])

  

    # Merge the two sorted pieces into a larger piece.



    print("Left side: ", left)

    print("Right side: ", right)

    merged = merge(left, right)

    print("Merged ", merged)

    return merged

The second function performs the actual task of merging the two sides using an 

iterative process. Here’s the code used to merge the two pieces:

def merge(left, right):

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

    # it means that this is an individual item and is

    # already sorted.

    if not len(left):

        return left

    if not len(right):

        return right

  



CHAPTER 7


Download 7,18 Mb.

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