Sunday, October 27, 2013

Quick sort, Merge sort, Radix sort, Shell sort in Python

QuickSort:

def quicksort(a,l,r):
    if l < r :
        i = l
        j = r
        pivot = a[i]
        while i < j:
            while a[i] < pivot:
                i = i + 1
            while a[j] > pivot:
                j = j - 1
            if i < j :
                t = a[i]
                a[i] = a[j]
                a[j] = t
        t = a[i]
        a[i] = pivot
        pivot = t
        quicksort(a,l,j-1)
        quicksort(a,j+1,r)
print ("How many Nos. ")
n=int(input())
print ('Enter the Nos. ')
a=[int(input()) for i in range(n)]
print("Accepted Array is ",a)
l=0
r=n-1
quicksort(a,l,r)
print ("Sorted Array is ",a)

MergeSort:

def merge(left, right):
    result = []
    i, j = 0, 0
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1

    result += left[i:]
    result += right[j:]
    return result

def mergesort(lst):

    if len(lst) <= 1:

        return lst

    middle = int(len(lst) / 2)
    left = mergesort(lst[:middle])
    right = mergesort(lst[middle:])
    return merge(left, right)
print ('How many Nos.')
n=int(input())
print ('Enter the Nos. -')
a=[int(input()) for i in range(n)]
print ('Sorted Array is -')
mergesort(a)
print (a)

ShellSort:

def shell(alist):
         count=len(alist)//2
         while count>0:
                   for start in range(count):
                             gap(alist,start,count)
                   print("increment", count,"list is",alist)
                   count=count//2
def gap(alist,start1,gap1):             
         for i in range (start1+gap1,len(alist),gap1):
                   current=alist[i]
                   pos=i
                   while pos>=gap1 and alist[pos-gap1]>current:
                             alist[pos]=alist[pos-gap1]
                             pos=pos-gap1
                   alist[pos]=current
alist=[2,0,5,9,1]
shell(alist)
print(alist)

RadixSort:

def r(a,k,i):
    c = i+1
    d = 1
    for s in range(c):
        re = (a[k] / d) % 10
        d *= 10
    return re
def radix(a,l,n):
    a1,b,c,d,e,f,g,h,i,j=[],[],[],[],[],[],[],[],[],[]
    buck=[a1,b,c,d,e,f,g,h,i,j]
    for i in range (l):
        for k in range (n):
            t = r(a,k,i)
            buck[t].append(a[k])
        a = []
        for s in range (10):
            sz = len(buck[s])
            for t in range (sz):
                a.append(buck[s][t])
            buck[s] = []
    return a
n = int(input( "Enter Total No of Elements - "))
a = [int(input())for i in range (n)]
lr = a[0]
for i in range (n):
    if a[i] > lr:
        lr = a[i]
l = len(str(lr))
print ('Sorted Array is -')
print ("a",radix(a, l, n))

No comments:

Post a Comment

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...