arr= [1, 12,100, 9, 5,7,6,8].

Added on - 18 Oct 2019

  • 9

    Pages

  • 1107

    Words

  • 105

    Views

  • 0

    Downloads

Trusted by +2 million users,
1000+ happy students everyday
Showing pages 1 to 3 of 9 pages
arr= [1, 12,100, 9, 5,7,6,8]crr= [3, 7, 15, 19, 20]brr= [2, 12, 15, 20, 45]drr= [2,5,8,10,16,77]err= [1, 3,14, 19, 45, 66]# making merge functiondef merge(a, b, m, n):c=[]i=0j=0#print("Sorting:")#print(a)#print(b)#print(m)#print(n)#print("while loop")while(i<m and j<n):#print(i)#print(a)#print(j)#print(b)if(a[i]<b[j]):c.append(a[i])i+=1else :c.append(b[j])
j+=1if (i>(m-1)):for x in range(j, len(b)):c.append(b[x])if (j>(n-1)):for x in range(i, len(a)):c.append(a[x])#print("Return sorted: " ,c)return c#print (merge(brr, crr, len(brr), len(crr)) )#print (merge(drr, err, len(drr), len(err)) )def mergeSort(a, st, lt):mid = int((st+lt)/2)#print(st,lt,mid)if (st ==mid):return aelse :left= a[st:mid]right= a[mid:lt]return merge(mergeSort(left, 0, len(left)), mergeSort(right, 0, len(right)), len(left),len(right))print(mergeSort(arr, 0, len(arr)))
class Node(object):def __init__(self, value):self.value=valueself.next=Noneself.prev=Noneclass List(object):def __init__(self):self.head=Noneself.tail=Nonedef insert(self,n,x):#Not actually perfect: how do we prepend to an existing list?if n!=None:x.next=n.nextn.next=xx.prev=nif x.next!=None:x.next.prev=xif self.head==None:self.head=self.tail=xx.prev=x.next=Noneelif self.tail==n:self.tail=xdef remove(self, val):"""Remove a node from the list."""current = self.head
desklib-logo
You’re reading a preview
Preview Documents

To View Complete Document

Click the button to download
Subscribe to our plans

Download This Document