In [ ]:
class listOpers(object):
'''Class to implement the following static list operations:
Insertion in a sorted list
Deletion of an element of list
Linear search
Binary search '''
def __init__(self,size):
self.l = [-1 for i in xrange(size)]
def insertElem(self,elem):
index = len(self.l)-1
for i in xrange(len(self.l)-1):
if self.l[i]>elem:
index = i
for i in xrange(index,len(self.l)-1):
self.l[i+1] = self.l[i]
self.l[index] = elem
def deleteElem(self,elem):
index = self.linearSearch(elem)
for i in xrange(index,len(self.l)-1):
self.l[i] = self.l[i+1]
def binarySearch(self,l,elem):
low = 0
high = len(l)-1
while low<high:
mid = (low + high)/2
if l[mid] == elem:
return mid
elif l[mid]>elem:
high = mid-1
elif l[mid]<elem:
low = mid+1
else:
return 'Not found'
def linearSearch(self,elem):
for i in xrange(len(self.l)):
if self.l[i]==elem:
return i