You are on page 1of 4

Assignment2

EE17B022-JEEVANA REDDY
October 15, 2020

[1]: import time


import random
import math
import numpy as np
import matplotlib.pyplot as plt

Q3
[2]: n = int(input("Enter number of elements : "))

Enter number of elements : 1000

[3]: A = [random.randint(-1000000, 1000000) for i in range(n)]

[4]: start = time.time()


B=[0]*10
for i in A:
if i<B[9]:
continue;
for j in range(10):
if i>B[j]:
for k in reversed(range(j,9)):
B[k+1]=B[k];
B[j]=i;
break;
print(B)
end = time.time()
print(f"Runtime of the program is {end - start}")

[999109, 998980, 998013, 988997, 985156, 985129, 983320, 982759, 982186, 981477]
Runtime of the program is 0.0010638236999511719

[5]: start = time.time()


A.sort(reverse=True)
A[0:10]
end = time.time()
print(f"Runtime of the program is {end - start}")

1
Runtime of the program is 0.00034308433532714844

[6]: if A[0:10] ==B:


print("10 largest numbers returned")

10 largest numbers returned

Q4
[7]: def karatsubaMult(x ,y):
x = str(x)
y = str(y)
#base case for recursion
if len(x) == 1 and len(y) == 1:
return int(x) * int(y)
if len(x) < len(y):
x = x.zfill(len(y))
elif len(y) < len(x):
y = y.zfill(len(x))

n = len(x)
if (n % 2) != 0:
n += 1
x = x.zfill(n)
y = y.zfill(n)

j = n//2

a = int(x[:j])
b = int(x[j:])
c = int(y[:j])
d = int(y[j:])
#recursively calculate
ac = karatsubaMult(a, c)
bd = karatsubaMult(b, d)
k = karatsubaMult(a + b, c + d)
A = (int(str(ac)))*(2**n)
B = (int(str(k - ac - bd)))*(2**j)
return A + B + bd

[8]: X = 10011011
Y = 10111010
start = time.time()
print(karatsubaMult(X,Y))
end = time.time()
print(f"Runtime of the program is {end - start}")

28830

2
Runtime of the program is 0.0007526874542236328

Q5
[9]: A0 = [random.randint(-1000000, 1000000) for k in range(0, 100)]
A1=[random.randint(-1000000, 1000000) for k in range(0, 150)]
A0.sort()
A1.sort(reverse=True)
B = A0 + A1

[10]: print(max(B))

999955

[11]: def binsearch(arr, a, b):


if a == b:
return arr[b]

if b == a + 1 and arr[b] >= arr[a]:


return arr[b];

if b == a + 1 and arr[b] < arr[a]:


return arr[a]

mid = (a + b)//2

if arr[mid] > arr[mid + 1] and arr[mid] > arr[mid - 1]:


return arr[mid]

if arr[mid] > arr[mid + 1] and arr[mid] < arr[mid - 1]:


return binsearch(arr, a, mid-1)
else:
return binsearch(arr, mid + 1, b)

[12]: start = time.time()


A = binsearch(B,0,len(A0)+len(A1))
end = time.time()
print(f"Runtime of the program is {end - start}")

Runtime of the program is 0.0001499652862548828

[13]: A

[13]: 999955

3
Q6
[22]: n = int(input("Enter number of elements : "))

Enter number of elements : 1000

[23]: A = [random.randint(-1000000, 1000000) for i in range(n)]

[24]: def mergesort(A):


n = len(A)
if n<= 1:
return A
left = mergesort(A[0:n//2])
right = mergesort(A[n//2:n])
B = [0]*n
i = j = 0
for k in range(0,n):
try:
if left[i] < right[j]:
B[k] = left[i]
i += 1
else:
B[k] = right[j]
j += 1
except:
return B
return B

[25]: start = time.time()


A=mergesort(A)
res = []
for i in A:
if i not in res:
res.append(i)
end = time.time()
print(f"Runtime of the program is {end - start}")

Runtime of the program is 0.018226146697998047

[26]: C=list(set(A))
C.sort()
res.sort()

[27]: if res==C:
print("sorted and removed")

sorted and removed

You might also like