You are on page 1of 7

the Session: Time of the

Date of Session:, to,


EX-I
1Analysis of Time and Space Complexity of Algorithms

Prerequisites:

.Basics of Data Structures and C Programming.

Basic knowledge about algorithms in C and Data Structures

Pre-Lab:

1)
During lockdown Mothi gets bored by his daily routine while scrolling YouTube he found an
algorithm that looks difierent. Mothi is very crazy about algorithms, but he cannot solve
algorithms of multiple loops so that he got struck and need your help to find the time
complexity of that algorithm.

Algoritm KLUint n)

int count=0;
for(int i-1;i<n;i=i*2) Log
for(int j=n;j>0:j=j/3) loq
{
for(int k=0;k<n;k++) - n+|

counttt

14o2 +lot+n+l+ 0rlosj


2t nloggnogn+loa"Keq )
2to+tnigs
TCon) OCm
Cp)- C+

J- Count n

- SCn)eO (n)
34
Y21-DAA Pacel3
21CS2214RB-DAA LABWORKBOOK
2) Klaus Michaelson is interviewer, so heprepared a bunch of questions for the students. He
on algorithms. Among all the questionsthe easiest focused
following C function is so can answer to this?
question is what is the time complexity of the

int recursive(int n)

if(n=1)
return (1);

else

return(recursive (n-1)+ recursive (n-1);}

T Cn) TCo-1)+ TCn-1)

2 TCn-1)
T Un)= 2 [2TCh-)) T ) - J

TCn-2)
2 TCn-3

T(0) 2TCn-)D
-

T(n) Tm-)
Co-Cn -D)
xTCm- 04)
2

-2

TCn)- O (27
p) OC)
Y21-DAA
21CS2214RB-DAALABWORKB00K
In-Lab
Caroline Forbes is an intelligent girl, every time she wins in any contest or programme, and solves
1) complex problems so I want to give her a challenge problem that is to Sort an array of strings
according to string lengths using insertion sort?
Input
You are beautiful looking
Output
You are looking beautiful

Source code:

t mcude < stelid th>


t mcludeegtdluo:h
titludi < shiwg
h

Cowrchatgrngs , i u t Coumt)
Void nuntion-sitt
Consee h a N _ e
foiLSte-ti= 13i<Count tt)
iCaunthor eey -SngsCIS
Sa-t=iAS csrgstjI»skln
wwiLefe Count44 Shlun
Clcay))
stongs t+0 =8hingstji3

Sríimg tf+ 13 =kays


t omavC)
Constchowringst 3-
you,'ane,"teaitgl
Lolns
8t2-t Cont seot CSUngs) {8icofCeha)
-

iw'wntin-gdit Csrg3, Con)3


Y21-DAA Page l5
FoCSizet izo iecount,itt)
primtfCs SrsmgsCJ
pff C°")

Oulpu
You are loolng beaulhuy

r
21CS2214RB-DAA LAB WORKBOOK

2) During the final skill exam teacher was given a problem and asked everyone to write the
to it, and advised that
try to
implement different approach from others.
a
algorithm
Question: Write an algorithm to calculate sum of first 'n' natural numbers
Mothi, one of the student in the
class thinking that his friends will write an efficient
that question. So, he wants writes a
worst approach to make that
algorithm to
to find whether the algorithm
algorithm written by Mothi for sum of first 'n' natural
as unique. So, the task is

and analyze its time


complexity. numbers is correct or not
Algorithm written by Mothi for Mothisum of first 'n' natural numbers
Algorithm SumOfNNaturalNums(int n)
int count=0;
for(i=1;i<=n;it+)
forg-1;j<-i;j+)-
-mCm-t)-D+o
countt+;

return count;

Source code:

20+3)

OC
p)C VSp D= 3-t
OCn) )
Counfn
21CS2214RB-DAA LAB WORKB00K

In-Lab:
1) Harry's Aunt and family treat him badly and make him work all the time. Dudley, his cousin got
homework from school so he as usual handed it over to Harry but Harry has a lot of work and his
own homework to do.
The homework is to solve the problems which are numbered in numerical he tries to solve
random question after solving random questions he did not put those questions in order Dudley
Will return in a time of n*logn Harry has to arrange them as soon as possible. Help Harry to
solve this problem so that he can go on and do his own homework.
[Note: Sort it using Quick Sort]
Example
Input
9
15,5,24,8,1,3,16,10,20
Output
1,3,5, 8, 10, 15, 16,20,24
Source code:

vudL <sdio.h
CCnhowvay T3,Mtutntv
Votd auiekSrt
wt pivst,tz,tenap
Capt <azht)
ptvot lats

(omay tîi z=amraCpa


pohile
tpiotI4|> =ufE)
lile Comayt>amay

teup -arayTol
ji3
awdy ci3 omat
amay Tu J temps

Y21 DAA
Pagel14
Lemp =awau tiI
avvay rj3 awray lpvt I3
=

Oray Tpvot J- teps


quiir sRt Comay, upt,f-1)
ouctkSoit Camadt1,1

ht mamt)
t i,n,away Eio3
SCom C'yd"4n)
CaDienitt
SCam yd.4arayt13

fos (ink o; ien^i+)


ptC/.d'", araytii)s
printt ()S
et wn0

Ourpuh

9
1S5 24 81 316 10 20
3 3 5 8 l0 S16 20 24

You might also like