Professional Documents
Culture Documents
Prathamesh Foa 52
Prathamesh Foa 52
Prathamesh Foa 52
Subject in charge
Head of the
Department
Date
1
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
INDEX
Sr.No. PRACTICAL NAME DATE
2 Write a Python program to sort n names using Quick sort algorithm. 18/11/21
Discuss the complexity of algorithms used.
4 Write a Python program for inserting an element into a binary tree 23/11/21
5 Find the smallest and largest number and get input from the user. 24/11/21
6 Write a Python program for finding the smallest and largest elements 26/11/21
in an array A of size n using Selection algorithm.
8 Write a Python program for checking whether a given graph G has a 02/12/21
simple path from source s to destination d. Assume the graph G is
represented using an adjacency matrix.
9 Write a Python program for finding the second largest element in an 07/12/21
array A of size n using Tournament Method.
2
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
3
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
O/P:-
4
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
O/P:-
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0
while i<len(lefthalf) and j<len(righthalf):
if lefthalf[i]<righthalf[j]:
nlist[k] = lefthalf[i]
i=i+1
else:
nlist[k] = righthalf[j]
j=j+1
k=k+1
while i<len(lefthalf):
5
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
nlist[k] = lefthalf[i]
i=i+1
k=k+1
while j<len(righthalf):
nlist[k] = righthalf[j]
j=j+1
k=k+1
print("Merging:",nlist)
nlist = [14,46,43,27,57,41,45,21,70]
mergeSort(nlist)
print(nlist)
O/P:-
6
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
def insert(self,data):
if self.data:
if data<self.data:
if self.left is None:
self.left=Node(data)
else:
self.left.insert(data)
elif data>self.dat:
if self.right is None:
self.right=Node(data)
else:
self.right.insert(data)
else:
self.data=data
def PrintInordertree(self):
if self.left:
self.left.PrintInordertree()
print(self.data),
if self.right:
self.right.PrintInordertree()
def PrintPreordertree(self):
print(self.data)
if self.left:
self.left.PrintPreordertree()
if self.right:
self.right.PrintPreordertree()
7
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
def PrintPostordertree(self):
if self.left:
self.left.PrintPostordertree()
if self.right:
self.right.PrintPostordertree()
print(self.data)
root=Node(10)
root.insert(4)
root.insert(13)
root.insert(1)
root.insert(5)
print("Inorder of tree")
root.PrintInordertree()
print("Preorder of tree")
root.PrintPreordertree()
print("Postorder of tree")
root.PrintPostordertree()
O/P:-
8
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
if arr[i]<min:
min = arr[i]
return min
def largestarr(arr,n):
max = arr[0]
for i in range(1,n):
if arr[i]>max:
max = arr[i]
return max
arr =[]
n =int(input("Enter no of element:"))
for i in range(n):
element = int(input("Enter an element:"))
arr.append(element)
print(arr)
ans = largestarr(arr,n)
print("Largest in given array is:",ans)
ans1 = smallestrarr(arr,n)
print("Smallest in given array is:",ans1)
O/P:-
9
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
def largestarr1(arr1,a):
max = arr1[0]
for i in range(1,a):
if arr1[i]>max:
max = arr1[i]
return max
arr1 = [15,25,55,60]
a = len(arr1)
ans1 = largestarr1(arr,a)
print("Largest in given array is:",ans1)
O/P:-
10
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
def strassen(a,b):
s = [b[0][1]-b[1][1],
a[0][0]+a[0][1],
a[1][0]+a[1][1],
b[1][0]-b[0][0],
a[0][0]+a[1][1],
b[0][0]+b[1][1],
a[0][1]-a[1][1],
b[1][0]+b[1][1],
a[0][0]-a[1][0],
b[0][0]+b[0][1]]
p = [a[0][0]*s[0],
s[1]+b[1][1],
s[2]*b[0][0],
a[1][1]*s[3],
s[4]*s[5],
s[6]*s[7],
s[8]*s[9]]
c = [[p[4]+p[3]-p[1]+p[5],p[0]+p[1]],
[p[2]+p[3],
p[4]+p[0]-p[2]-p[6]]]
print("Strassens matrix")
print(s)
print(p)
print(c)
strassen(x,y)
11
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
O/P:-
12
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
if visited[i]==False:
self.printAllPathsUtil(i,d,visited,path)
path.pop()
visited[u]=False
def printAllPaths(self,s,d):
visited=[False]*(self.v)
path=[]
self.printAllPathsUtil(s,d,visited,path)
g = Graph(4)
g.addEdge(0,1)
g.addEdge(0,2)
g.addEdge(0,3)
g.addEdge(2,0)
g.addEdge(2,1)
g.addEdge(1,3)
s=2
d=3
print("Following are all different path from %d to %d:",s,d)
g.printAllPaths(s,d)
O/P:-
13
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
O/P:-
14
FOA PRACTICAL JOURNAL
Name:-Prathamesh P. Sonkamble Roll No:-52
15