Professional Documents
Culture Documents
PRACTICAL FILE
Submitted By
PAWAN KUMAR
Roll Number:
19-811
Submitted To
Dr. Mandeep kaur
INDEX
S.
No. Name of Practical/Program Page no
* Introduction to Algorithm 3-6
1 Write a python program for merge sort and analyse the run 7-9
time and no of function calls for different input
2 Write a python program for quick sort and analyse the run 10-12
time and no of function calls for different input
3 Write a python program for Linear Search and analyse best 13-14
case , worst case and average case run time for different
input
INTRODUCTION TO ALGORITHM
Characteristics of Algorithm
Asymptotic Notation
Big-Omega Notation, Ω
The notation Ω(n) is the formal way to express the lower bound of an
algorithm's running time. It measures the best case time complexity or the
best amount of time an algorithm can possibly take to complete.
f(n)>=c*g(n)
Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and
the upper bound of an algorithm's running time. It is represented as
follows −
Pawan (19-811) 6
Small-o
Small-o, commonly written as o, is an Asymptotic Notation to denote the
upper bound (that is not asymptotically tight) on the growth rate of
runtime of an algorithm.
f(n) is o(g(n)), if for any real constant c (c > 0), f(n) is < c g(n) for every
input size n (n > 0).
The definitions of O-notation and o-notation are similar. The main
difference is that in f(n) = O(g(n)), the bound f(n) <= g(n) holds for some
constant c > 0, but in f(n) = o(g(n)), the bound f(n) < c g(n) holds for all
constants c > 0.
Small-omega
Small-omega, commonly written as ω, is an Asymptotic Notation to
denote the lower bound (that is not asymptotically tight) on the growth
rate of runtime of an algorithm.
f(n) is ω(g(n)), if for any real constant c (c > 0), f(n) is > c g(n) for every
input size n (n > 0).
The definitions of Ω-notation and ω-notation are similar. The main
difference is that in f(n) = Ω(g(n)), the bound f(n) >= g(n) holds for some
constant c > 0, but in f(n) = ω(g(n)), the bound f(n) > c g(n) holds for all
constants c > 0.
Pawan (19-811) 7
Code:
import random
import time
Start = 1
Stop = 8
limit = 4
start_time = time.time()
def mergeSort(R):
mergeSort.counter +=1
if len(R)>1:
mid = len(R)//2
lefthalf = R[:mid]
righthalf = R[mid:]
#recursion
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
mergeSort(R)
print("\nthe sorted nos :",R)
print("time taken %s seconds ---" % (time.time() - start_time))
f= open("merge.txt","a")
f.write("\n No of inputs : %s " %limit)
f.write(" \nTime taken : %s seconds ---\n" % (time.time() -
start_time))
f.write(" Funcation calls : %s" %mergeSort.counter)
f.write(" \
n-----------------------------------------------------------------------------------
--------------------------------------------------------------\n")
f.close()
# start = time.clock()
#end = time.clock()
#times.append(end-start)
Pawan (19-811) 9
Output:
Pawan (19-811) 10
Code :
import random
import time
Start = 1
Stop = 2000000
limit = 1000000
start_time = time.time()
quicksort.counter +=1
quicksort(R, start, p)
quicksort(R, p + 1, end)
Pawan (19-811) 11
pivot = R[start]
i = start + 1
j = end - 1
while True:
i=i+1
j=j-1
if i <= j:
else:
return j
quicksort.counter =0
quicksort(R, 0, len(R))
print(R)
f= open("quick.txt","a")
f.write(" \
n----------------------------------------------------------------------------------------------------------
---------------------------------------\n")
f.close()
# start = time.clock()
#end = time.clock()
#times.append(end-start)
Output:
Pawan (19-811) 13
Code:
Output:
Pawan (19-811) 15
Code :
def maxSubArraySum(a,size):
max_so_far = 0
max_ending_here = 0
return max_so_far
a = [-2, -3, 4, -1, -2, 1, 5, -3]
print("Maximum contiguous sum is" , maxSubArraySum(a,len(a)))
Output:
Pawan (19-811) 16
Code :
max = arr[0]
result = 0
for i in range(1, n):
return result
arr = [2, 3, 3, 5, 3, 4, 1, 7]
n = len(arr)
k=8
print("The maximum repeating number is",maxRepeating(arr, n, k))
Output:
Pawan (19-811) 17
Code :
cnt = 0
for i in range(0, n) :
for j in range(i + 1, n) :
if arr[j] > arr[j - 1] :
cnt += 1
break
return cnt
arr = [1, 2, 2, 4]
n = len(arr)
print ("Count of strictly increasing subarrays is",
countIncreasing(arr, n))
Output:
Pawan (19-811) 18
Code:
def findSmallest(arr, n):
res = 1
else:
break
return res
arr1 = [1, 3, 4, 5]
n1 = len(arr1)
print(findSmallest(arr1, n1))
n2 = len(arr2)
print(findSmallest(arr2, n2))
arr3= [1, 1, 1, 1]
n3 = len(arr3)
print(findSmallest(arr3, n3))
arr4 = [1, 1, 3, 4]
n4 = len(arr4)
print(findSmallest(arr4, n4))
Output: