You are on page 1of 8

REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 2

Experiment 2
Design and Analysis of Algorithms Lab
20CP209P

Part 1: Merge Sort

AIM: To perform merge sort on an array of integers and analyse it’s time complexity.

THEORY: In merge sort, we recursively divide the array into half and when we reach unit
arrays, we start merging them again in sorted manner. The mechanism for the merge goes
like this: we iterate through the left and right half simultaneously and append the smaller of
the two current elements into a newly cleared final array, once we are completely through
with one of the arrays, we append the rest of the elements of the second array at the end of
the final array.

ALGORITHM:

1
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 2

CODE: (in python)


#merge sort
def msort(a):
if len(a)>1:
m=int(len(a)/2)
x=a[:m]
y=a[m:]
msort(x)
msort(y)
i=j=k=0
while i<m and j<len(y):
if x[i]<y[j]:
a[k]=x[i]
i+=1
else:

2
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 2

a[k]=y[j]
j+=1
k+=1
while i<m:
a[k]=x[i]
k+=1
i+=1
while j<len(y):
a[k]=y[j]
k+=1
j+=1
n=int(input('enter length of array'))
l=[0]*n
print("enter ",n," elements:")
for i in range(n):
l[i]=int(input())
msort(l)
print(l)

3
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 2

OUTPUT:

ANALYSIS:
here, the no. of steps needed to perform this will not change for any kind of input
therefore, time complexity for best,worst,average case is the same.
T(n)=O(nlogn)

4
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 2

Part 2: Quick Sort

AIM: To perform quick sort on an array of integers and analyse it’s time complexity.

THEORY: In this sort, we choose a pivot element and divide the array (except the pivot)
into 2 parts according to whether the elements are greater than the pivot and which are
smaller. This is done recursively for each sub array until we reach a unit array. Then we start
merging the parts by appending the array of the smaller elements first, then the pivot and then
the second array. This is also done in the recursive calls and the final sorted array is returned
each time.

ALGORITHM:

CODE: (in python)


#quick sort
def qsort(a):

5
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 2

if len(a)>1:
e=a[0]
x=[]
y=[]
a.remove(e)
for i in a:
if i<e:
x.append(i)
else:
y.append(i)
qsort(x)
qsort(y)
a.clear()
for i in x:
a.append(i)
a.append(e)
for i in y:
a.append(i)
else:
return
n=int(input('enter length of array'))
l=[0]*n
print("enter ",n," elements:")
for i in range(n):
l[i]=int(input())
qsort(l)
print(l)

OUTPUT:

6
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 2

ANALYSIS:
when the array is completely sorted or reversely sorted (when pivot element is first or last
element), it is the worst case in the algorithm. (O(n^2))
i.e. when the pivot element is continuously the largest or smallest element in the array we are
considering.
the best case is when the pivot element is the middle element and divides the array into 2
almost of eqaul length arrays. (O(nlogn))

example:
array: 1 2 3 4 5 6
no. of steps:

7
REHA SHAH 21BCP148 G5 DAA LAB EXPERIMENT 2

=3+7+6+2+(6+6+10+1+1+11)+(6+5+8+1+1+9)+2+(6+4+6+1+1+7)+2+(6+3+4+1+1+5)+2+(
6+2+2+1+1+3)+2+2
=18+10+35+30+25+20+15
=153 steps

array: 6 5 4 3 2 1
no. of steps:
=3+7+6+2+(6+6+10+1+1+11)+(6+5+8+1+1+9)+2+(6+4+6+1+1+7)+2+(6+3+4+1+1+5)+2+(
6+2+2+1+1+3)+2+2
=18+10+35+30+25+20+15
=153 steps

array: 4 2 1 3 5 6
=3+7+6+2+(6+6+10+1+7+1+5)+(6+3+4+1+3+1+3)+2+2+(6+2+2+1+1+3)+2+2
=18+8+36+21+15
=98 steps

You might also like