You are on page 1of 12

Exam Seat No:

Satish Pradhan Dnyanasadhana College


Thane

Certificate

This is to certify that Mr./Miss.: Mayuresh Kasar of FYBSc Computer Science


(Semester-II) Class has successfully completed all the practical work in subject
Design & Analysis of Algorithms, under the guidance of Prof. Kavita Chouk
(subject in charge) during Year 2021-22 in partial fulfillment of Computer
Science Practical Examination conducted by University of Mumbai.

Subject in charge Head of the Department

Date
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

Sr.
Index Date Sign
No.
Programs on 1-d arrays like - sum of elements of
array, searching an element in array, finding
minimum and maximum element in array, count the
1 number of even and odd numbers in array. For all
such programs, also find the time complexity,
compare if there are multiple methods.
Programs on 2-d arrays like row-sum, column-sum,
sum of diagonal elements, addition of two matrices ,
2 multiplication of two matrices. For all such programs,
also find the time complexity, compare if there are
multiple methods.
Program to create a list-based stack and perform various
3 stack operations.

Programs to sort elements of list by using various


4 algorithms like bubble, selection sort, and insertion sort.
Compare the efficiency of algorithms.
Programs on recursion like factorial, fibonacci, tower of
5 hanoi. Compare algorithms to find factorial/fibonacci
using iterative and recursive approaches.
Program to implement merge sort, Straseen‟s Matrix
6 Multiplication using D-n-C Algorithm and to understand
time complexity.

2
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

1.Programs on 1-d arrays like - sum of elements of array, searching an


element in array, finding minimum and maximum element in array, count the
number of even and odd numbers in array. For all such programs, also find
the time complexity, compare if there are multiple methods.
1.1) Sum of elements
Code:
s=int(input("Enter the number"))
sum=0
for num in range(s + 1):
sum=sum+num
print("Result of first n number ",sum)
Output:
Enter the number3
Result of first n number 0
Result of first n number 1
Result of first n number 3
Result of first n number 6
1.2) Searching an element in array
Code:
list1=[10,12,20,9]
x=10
for i in range(0,len(list1)):
print("Searching for element: ", end=" ")
if (list1[i] == x):
print("found");
break;
else:
print( "not found")
break;
Output:
Searching for element: 12
found

3
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

1.3) Finding minimum element in array


Code:
list=[10,3,5,20,9,25]
min=list[0]
for i in list:
if i<min:
min=i
print("list of values:",list)
print("min value in list:",min)
Output:
list of values: [10, 3, 5, 20, 9, 25]
min value in list: 3
1.4) Finding maximum element in array
Code:
list=[10,3,5,20,9,25]
max=list[0]
for i in list:
if i>max:
max=i
print("list of values:",list)
print("max value in list:",max)
Output:
list of values: [10, 3, 5, 20, 9, 25]
max value in list: 25
1.5) Count the number of even and odd numbers in array
Code:
listName =['hello',20,30,'Efc',50]
print("the list is:"+str(listName))
count = 0
for i in listName:
count=count+1
print(" Length of list using naive method is:" + str(count))
Output:
the list is:['hello', 20, 30, 'Efc', 50]
Length of list using naive method is:5

4
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

2. Programs on 2-d arrays like row-sum, column-sum, sum of diagonal


elements, addition of two matrices , multiplication of two matrices. For all
such programs, also find the time complexity, compare if there are multiple
methods.
2.1) Addition of two matrices
Code:
a=[[1,2,3],[4,5,6],[7,8,9]]
b=[[3,4,5],[6,7,1],[2,4,6]]
result=[[a[i][j]+b[i][j] for j in range
(len(a[0]))] for i in range(len(a))]
for r in result:
print(r)
Output:
[10, 12, 7][9, 12, 15]
2.2) Multiplication of two matrices
Code:
a=[[1,2,3],[4,5,6],[7,8,9]]
b=[[3,4,5],[6,7,1],[2,4,6]]
multiresult=[[0,0,0],[0,0,0],[0,0,0]]
for m in range(len(a)):
for n in range(len(b[0])):
for o in range(len(b)):
multiresult[m][n] +=a[m][n]*b[m][n]
print("the multiplication result of matrix and bis:")
for res in multiresult:
print(res)
Output:
the multiplication result of matrix and bis:
[9, 24, 45][72, 105, 18][42, 96, 162]

5
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

3. Program to create a list-based stack and perform various stack operations.


Code:
class Stack:
def __init__(self):
self.item=[]
self.size=0
def push(self,value):
self.item.append(value)
self.size+=1
def pop(self):
if self.isEmpty():
print("stack is empty")
else:
print("popped no from stack is",self.item.pop())
self.size-=1
def isEmpty(self):
if(self.size==0):
return True
else:
return False
def display(self):
print("starting from the top the stack element are positional as:")
self.i=self.size=1
while self.i>=0:
print(self.item[self.i])
self.i-=1
def peek(self):
if self.isEmpty():
print("stack is empty")
else:
print("top most element in the stack is ",self.item[self.size-1])
def length(self):
if self.isEmpty():
print("stack is empty")
else:
print("the no of item in stack are",self.size)
s=Stack()
print("menu")
print("1: push\n2: pop\n2: peek\n4: display\n5: length\n6: exit")

6
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

ch=int(input("press your choice"))


while ch<=5:
if ch==1:
val=int(input("enter the number to be inserted in stack"))
s.push(val)
elif ch==2:
s.pop()
elif ch==3:
s.peek()
elif ch==4:
s.display()
elif ch==5:
s.length()
else:
exit
ch=int(input("press your choice"))
Output:
menu
1: push
2: pop
2: peek
4: display
5: length
6: exit
press your choice1
enter the number to be inserted in stack3
press your choice1
enter the number to be inserted in stack6
press your choice2
popped no from stack is 6
press your choice2
popped no from stack is 3
press your choice5
stack is empty
press your choice6

7
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

4. Programs to sort elements of list by using various algorithms like bubble,


selection sort, and insertion sort. Compare the efficiency of algorithms.
4.1) Sorting elements of list by using selection sort algorithm.
Code:
def selectionSort(array, size):
for step in range(size):
min_idx = step
for i in range(step + 1, size):
# to sort in descending order, change > to < in this line
# select the minimum element in each loop
if array[i] < array[min_idx]:
min_idx = i
# put min at the correct position
(array[step], array[min_idx]) = (array[min_idx], array[step])
data = [-2, 45, 0, 11, -9]
size = len(data)
selectionSort(data, size)
print('Sorted Array in Ascending Order:')
print(data)
Output:
Sorted Array in Ascending Order:
[-9, -2, 0, 11, 45]
4.2) Sorting elements of list by using insertion sort algorithm.
Code:
def insertionSort(mylist):
for i in range(1,len(mylist)):
current=mylist[i]
gap=i
while gap>-0 and mylist[gap-1]>current:
mylist[gap]=mylist[gap-1]
gap=gap-1
mylist[gap]=current
return mylist
mylist=[20,10,30,60,40,50,55]
print(insertionSort(mylist))
Output:
[10,20,30,40,50,55,60]

8
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

5. Programs on recursion like factorial, fibonacci, tower of hanoi. Compare


algorithms to find factorial/fibonacci using iterative and recursive
approaches.
5.1) Factorial of number using iterative and recursive.
Code:
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 7
result = factorial(num)
print("The factorial of ",num,"is",result)
Output:
The factorial of 7 is 5040
5.2) Fibonacci series using iterative and recursive.
Code:
def fib(number):
if(number==0):
return 0
elif(number==1):
return 1
else :
return(fib(number-2)+fib(number-1))
number=int(input("enter thr range number:"))
for n in range(0,number):
print(fib(n), end=" ")
Output:
enter thr range number:10
0 1 1 2 3 5 8 13 21 34

9
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

5.3) Tower of hanoi using iterative and recursive.


Code:
def movtower(height,source,destination,intermidiate):
if height>=1:
movtower(height-1,source,intermidiate,destination)
movedisk(source,destination)
movtower(height-1,intermidiate,destination,source)
def movedisk(fp,tp):
print("moving disk from ",fp,"to",tp)
movtower(3,"a","b","c")
Output:
moving disk from a to b
moving disk from a to c
moving disk from b to c
moving disk from a to b
moving disk from c to a
moving disk from c to b
moving disk from a to b

10
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

6. Program to implement merge sort, Straseen‟s Matrix Multiplication using


D-n-C Algorithm and to understand time complexity.
Code:
def mergeSort(nlist):
print("Splitting ",nlist)
if len(nlist)>1:
mid = len(nlist)//2
lefthalf = nlist[:mid]
righthalf = nlist[mid:]

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):


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)
Output:
Splitting [14, 46, 43, 27, 57, 41, 45, 21, 70]
Splitting [14, 46, 43, 27]

11
Satish Pradhan Dnyanasadhana College, Thane [ A. Y. 2021 – 2022]
Name: Mayuresh Kasar Roll No.: 35
Program: FY B.Sc. CS (sem II) Subject: Design & Analysis of Algorithms (PR)

Splitting [14, 46]


Splitting [14]
Merging [14]
Splitting [46]
Merging [46]
Merging [14, 46]
Splitting [43, 27]
Splitting [43]
Merging [43]
Splitting [27]
Merging [27]
Merging [27, 43]
Merging [14, 27, 43, 46]
Splitting [57, 41, 45, 21, 70]
Splitting [57, 41]
Splitting [57]
Merging [57]
Splitting [41]
Merging [41]
Merging [41, 57]
Splitting [45, 21, 70]
Splitting [45]
Merging [45]
Splitting [21, 70]
Splitting [21]
Merging [21]
Splitting [70]
Merging [70]
Merging [21, 70]
Merging [21, 45, 70]
Merging [21, 41, 45, 57, 70]
Merging [14, 21, 27, 41, 43, 45, 46, 57, 70]
[14, 21, 27, 41, 43, 45, 46, 57, 70]

12

You might also like