You are on page 1of 41

Pat

elMi
hit 1
800401
07034

ADAPRACTI
CALLI
ST

QUE01:I mplementati
onandTimeanal
ysi
sofs or
tingal
gor
ith
ms:Bubbl
e
sor
t,Sel
ecti
ons or
t,Ins
ert
ions
ort,
MergesortandQuicks
ort

A.BubbleSor
t
#inc
lude<st
dio.
h>
#inc
lude<st
dbool.
h>
#defi
neMAX1 0

i
ntl
ist
[MAX]={
1,8,
4,
6,
0,
3,
5,
2,
7,
9};

v
oiddisplay(
)
{
inti;
printf
("[
");

//navi
gatethr
oughallit
ems
for
(i=0;i<MAX;i++
)
{
pri
ntf(
"%d",
li
st[i
]);
}

pr
int
f("
]\
n")
;
}

v
oidbubbleSor
t()
{
intt
emp;
inti
,j;
boolswapped=fals
e;
//loopthroug
hallnumber
s
for(
i=0;i<MAX-1;i+
+)

ADA(
215
0703) 1
Pat
elMi
hit 1
800401
07034

{
swapped=false;
//loopthr
oughnumber sfal
lingah
ead
for(
j=0;j<MAX- 1
-i;
j++)
{
pr
int
f("
Items c ompared: [ %d, %d ] "
,
l
ist
[j]
,l
ist
[j+1]
);

//c
hec
kifnex
tnumberi
sles
sert
hanc
urr
ent
no
// swapt
henumber
s.
//(Bubbl
eupthehi
ghes
tnumber
)

i
f(l
ist
[j]>lis
t[j
+1])
{
temp=l is
t[j
];
list
[j]=li
st[
j+1]
;
list
[j+1
]=temp;

swapped=true;
pri
ntf(
" =
> swapped [
%d,
%d]
\n"
,l
ist
[j]
,l
ist
[j+
1]);
}
else
{
pri
ntf(
"=>notswapped\n"
);
}

//ifnonumberwasswappedth
atmeans
// arrayi
ssort
ednow,breakt
hel
oop.
if
(!s
wapped)

ADA(
215
0703) 2
Pat
elMi
hit 1
800401
07034

{
br
eak;
}

pr
int
f("
Iter
ati
on%d#:
",
(i
+1)
);
di
spl
ay();
}
}

v
oidmain()
{
pr
int
f("
InputAr
ray
:")
;
di
spl
ay();
pr
int
f("
\n"
);

bubbl
eSort(
);
pri
ntf(
"\
nOutputAr
ray
:")
;
dis
play()
;
}

Out
put:
I
nputArray:[
1846035279]
I
temsc ompared:
[1,
8]=>notswapped
I
temsc ompared:
[8,4]=>s
wapped[4,8]
I
temsc ompared:
[8,6]=
>swapped[6,8]
I
temsc ompared:
[8,0]=>s
wapped[0,8]
I
temsc ompared:
[8,3]=>s
wapped[3,8]
I
temsc ompared:
[8,5]=
>swapped[5,8]
I
temsc ompared:
[8,2]=>s
wapped[2,8]
I
temsc ompared:
[8,7]=>s
wapped[7,8]
I
temsc ompared:
[8,9]=
>notswapped
I
ter
ation1#:[1460352789]
I
temsc ompared:
[1,
4]=>notswapped

ADA(
215
0703) 3
Pat
elMi
hit 1
800401
07034

I
temscompared:
[4,6]=>notswapped
I
temscompared:
[6,0]=>swapped[0,6]
I
temscompared:
[6,3]=>swapped[3,6]
I
temscompared:
[6,5]=
>s wapped[5,
6 ]
I
temscompared:
[6,2]=>swapped[2,6]
I
temscompared:
[6,7]=>notswapped
I
temscompared:
[7,8]=>notswapped
I
ter
ation2#:
[1403526789]
I
temscompared:
[1,
4]=>nots wapped
I
temscompared:
[4,0]=>swapped[0,4]
I
temscompared:
[4,3]=>swapped[3,4]
I
temscompared:
[4,5]=>notswapped
I
temscompared:
[5,2]=>swapped[2,5]
I
temscompared:
[5,6]=
>nots wapped
I
temscompared:
[6,7]=>notswapped
I
ter
ation3#:
[1034256789]
I
temscompared:
[1,
0]=>s wapped[0,1]
I
temscompared:
[1,
3]=>nots wapped
I
temscompared:
[3,4]=>notswapped
I
temscompared:
[4,2]=>swapped[2,4]
I
temscompared:
[4,5]=>notswapped
I
temscompared:
[5,6]=
>nots wapped
I
ter
ation4#:
[0132456789]
I
temscompared:
[0,1]=
>nots wapped
I
temscompared:
[1,
3]=>nots wapped
I
temscompared:
[3,2]=>swapped[2,3]
I
temscompared:
[3,4]=>notswapped
I
temscompared:
[4,5]=>notswapped
I
ter
ation5#:
[0123456789]
I
temscompared:
[0,1]=
>nots wapped
I
temscompared:
[1,
2]=>nots wapped
I
temscompared:
[2,3]=>notswapped
I
temscompared:
[3,4]=>notswapped

ADA(
215
0703) 4
Pat
elMi
hit 1
800401
07034

Out
putAr
ray
:[0123456789]

A.Select
ionSort:
#incl
ude<stdio.
h>
#incl
ude<stdbool.
h>
#defi
neMAX7

i
nti
ntAr
ray
[MAX]={
4,6
,3,
2,
1,
9,
7};

v
oi dpri
ntl
ine(
intc
ount
)
{
inti;

f
or(
i=0;
i<c
ount-
1;i
++)
{
pr
int
f("
=")
;
}

pr
int
f("
=\
n")
;
}

v
oi ddi
splay()
{
inti;
print
f("
[")
;

//navi
gatet
hroughal
lit
ems
for
(i=0;
i<MAX;i++
)
{
pri
ntf(
"%d",
int
Arr
ay[
i])
;
}

ADA(
215
0703) 5
Pat
elMi
hit 1
800401
07034

pr
int
f("
]\
n")
;
}

v
oi ds
elec
tionSort(
)
{
inti
ndexMin,i
,j
;

//loopt
hroughallnumber
s
for
(i=0;
i<MAX- 1;i+
+)
{
//s etc
urrentel
ementasmi
nimum
indexMin=i;

//checktheelementtobemi ni
mum
for
(j=i+1
;j<MAX;j++)
{
if
(int
Array[j
]<int
Array
[index
Min]
)
{
i
ndexMin=j;
}
}

i
f(i
ndexMin!=i)
{
pri
ntf(
"It
emss
wapped:
[%d,
%d]
\n"
,
int
Arr
ay[
i],
int
Array[i
ndex
Mi n]);

//s wapthenumbers
inttemp=intArr
ay[i
ndexMin]
;
intArr
ay[
indexMin]=int
Arr
ay[i]
;
intArr
ay[
i]=temp;
}

ADA(
215
0703) 6
Pat
elMi
hit 1
800401
07034

pr
int
f("
Iter
ati
on%d#:
"
,(i
+1)
);
di
spl
ay();
}
}

v
oidmai n()
{
pri
ntf(
"InputArr
ay:"
);
dis
play();
pri
ntli
ne(50);
selec
tionSort
();
pri
ntf(
"OutputArray
:")
;
dis
play();
pri
ntli
ne(50);
}

Out
put:
I
nputAr r
ay :
[4632197]
I
temss wapped:[4,1]
I
terat
ion1 #:
[1632497]
I
temss wapped:[6,
2]
I
terat
ion2#:[1236497]
I
terat
ion3#:[1236497]
I
temss wapped:[6,
4]
I
terat
ion4#:[1234697]
I
terat
ion5 #:
[1234697]
I
temss wapped:[9,
7]
I
terat
ion6 #:
[1234679]
Out
putAr ray:[
1234679]

B.Inser
tionSort
:
#incl
ude<stdio.
h>
#incl
ude<stdbool.
h>

ADA(
215
0703) 7
Pat
elMi
hit 1
800401
07034

#def
ineMAX7

i
nti
ntAr
ray
[MAX]={
4,6
,3,
2,
1,
9,
7};

v
oidpr
int
li
ne(
intc
ount
)
{
i
nti
;

f
or(
i=0;i<count-
1;
i+
+)
{
pr
intf
("="
);
}

pr
int
f("
=\
n")
;
}

v
oiddisplay(
)
{
inti;
printf
("[
");

//navi
gatethr
oughallit
ems
for
(i=0;
i<MAX; i
++)
{
pri
ntf(
"%d",
int
Array[i
]);
}

pr
int
f("
]\
n")
;
}

v
oidi
nser
tionSort
()
{
intvalueToI
nser
t;

ADA(
215
0703) 8
Pat
elMi
hit 1
800401
07034

i
nth
olePos
iti
on;
i
nti
;

//loopt
hroug
hal
lnumber
s
for
(i=1;
i<MAX;i
++)
{

//sel
ectavaluetobeins
erted.
val
ueToI
nser
t=i nt
Arr
ay[i
];

//sel
ectth
ehol
epos
iti
onwh
erenumberi
stobei
nser
ted
hol
ePosi
ti
on=i;

//chec kifpreviousno.islarg
ert hanvaluet obei nsert
ed
whi
le (holePos i
tion > 0 && i ntArray[
h olePosi
tion-1] >
val
ueToI
nser t
)
{
intArr
ay [
h olePos
iti
on]=i
ntAr r
ay[holePosit
ion-1];
holePos i
tion--
;
printf
("itemmov ed: %d\
n",intAr
ray [hol
ePos i
ti
on]);
}

if(
h ol
ePos i
tion!=i
)
{
pr i
ntf(
"item i
nsert
ed:%d,atpos i
ti
on:%d\
n",
v
alueToI
nser
t,
hol
ePos i
tion);
//i ns
er tth
enumberath oleposi
tion
intArr
ay [h
olePosit
ion]=val
ueToInser
t;
}

pr
int
f("
Iter
ati
on%d#:
"
,i)
;
di
spl
ay();

ADA(
215
0703) 9
Pat
elMi
hit 1
800401
07034

}
}

v
oi dmain()
{
print
f("I
nputArray:"
);
displ
ay ()
;
print
li
ne( 50)
;
inser
tionSort(
);
print
f("OutputArr
ay:")
;
displ
ay ()
;
print
li
ne( 50)
;
}

Output:
I
nputArray :[
4632197]
I
terati
on1 #:[
4632197]
it
emmov ed: 6
it
emmov ed: 4
it
emi nserted:3,atpos
iti
on:
0
I
terati
on2#: [
3462197]
it
emmov ed: 6
it
emmov ed: 4
it
emmov ed: 3
it
emi nserted:2,atpos
iti
on:
0
I
terati
on3#: [
2346197]
it
emmov ed: 6
it
emmov ed: 4
it
emmov ed: 3
it
emmov ed: 2
it
emi nserted:1,
atpositi
on:
0
I
terati
on4#: [
1234697]
I
terati
on5 #:[
1234697]

ADA(
215
0703) 1
0
Pat
elMi
hit 1
800401
07034

it
emmov ed: 9
it
emi nserted:7,atpos
iti
on:
5
I
terati
on6 #:[
1234679]
OutputArray:[1234679]

C.MergeSort
:
#inc
lude<
stdi
o.h
>
#defi
nemax10

i
nta[
11]={1
0,1
4,1
9,26
,27,
31,
33,
35,
42,
44,
0};
i
ntb[
10];

v
oidmerg
ing(i
ntlow,
intmi
d,i
nth
igh
)
{
i
ntl1
,l2,i
;

f
or(
l1=low,l2=mid+1 ,
i=l
ow;
l1<
=mi
d&&l
2<=h
igh
;i+
+)
{
if
(a[l1
]<=a[l2]
)
b[i
]=a[ l
1++
];
else
b[i
]=a[ l
2++]
;
}

whi
le(l
1<=mid)
b[
i++]=a[
l1+
+];

whi
le(l
2<=hi
gh)
b[
i++]=a[
l2+
+];

for
(i=l
ow;i<
=hi
gh;
i++
)
a[i
]=b[i
];
}

ADA(
215
0703) 1
1
Pat
elMi
hit 1
800401
07034

v
oids
ort(
intl
ow,
inth
igh
)
{
intmid;

if
(low<high)
{
mid=( l
ow+h igh
)/2;
sor
t(l
ow, mid);
sor
t(mid+1,hi
gh);
merging(
low,mid,hi
gh)
;
}
else
{
ret
urn;
}
}

i
ntmain(
)
{
i
nti;

pr
int
f("
Lis
tbef
ores
ort
ing
\n"
);

for
(i=0;i<
=max;i+
+)
pri
ntf
("%d"
,a[
i])
;

s
ort
(0,
max
);

pr
int
f("
\nLi
staf
ters
ort
ing
\n"
);

for
(i=0;i<
=max;i+
+)
pri
ntf
("%d"
,a[
i])
;

ADA(
215
0703) 1
2
Pat
elMi
hit 1
800401
07034

Output
:
Li
stbefor
esorti
ng
101419262731333542440
Li
staft
ersort
ing
010141926273133354244

D.QuickSor
t:
#inc
lude<st
dio.
h>
#inc
lude<st
dbool.
h>
#defi
neMAX7

i
nti
ntAr
ray
[MAX]={
4,6
,3,
2,
1,
9,
7};

v
oidpr
int
li
ne(
intc
ount
)
{
i
nti
;

f
or(
i=0;i<count-
1;
i+
+)
{
pr
intf
("="
);
}

pr
int
f("
=\
n")
;
}

v
oiddisplay(
)
{
inti;
printf
("[
");

//nav
igat
eth
roug
hal
lit
ems

ADA(
215
0703) 1
3
Pat
elMi
hit 1
800401
07034

f
or(
i=0;i<MAX;i
++)
{
pr
intf
("%d"
,i
ntAr
ray
[i]
);
}

pr
int
f("
]\
n")
;
}

v
oids
wap( i
ntnum1,intnum2)
{
inttemp=i nt
Array[num1];
intArr
ay[num1]=intArray
[num2]
;
intArr
ay[num2]=t emp;
}

i
ntpar
tit
ion(i
ntleft,i
ntr
igh
t,intpi
vot
)
{
i
ntleft
Pointer=lef
t-1;
i
ntrigh
tPointer=ri
ght
;

whi
le(
true)
{
while(
int
Array
[++lef
tPoi
nter]<pi
vot)
{
//donothi
ng
}
while(
rig
htPoi
nter>0&&i ntAr
ray[
--
rig
htPoi
nter
]>
pi
vot)
{
//donothi
ng
}

i
f(l
eft
Poi
nter>
=ri
ght
Poi
nter
)

ADA(
215
0703) 1
4
Pat
elMi
hit 1
800401
07034

{
br
eak;
}
el
se
{
pri
ntf
(" item swapped :
%d,
%d\n"
,
int
Arr
ay[l
eft
Pointer
],i
ntArr
ay[r
igh
tPoi
nter
]);
swap(
lef
tPoi
nter,
rig
htPoint
er);
}
}
pri
ntf(" pi
vot swapped :
%d,
%d\
n",
int
Array[l
eftPointer],
intAr
ray
[ri
ght
]);
swap(left
Pointer,r
ight
) ;
pri
ntf("
UpdatedAr r
ay:");
displ
ay();
retur
nl ef
tPointer;
}

v
oidquickSort
( i
ntleft
, intrigh
t)
{
if
(right
-left<=0)
{
return;
}
else
{
intpivot=i ntArray[
right
];
intpartit
ionPoi nt=par t
iti
on(l
eft,r
igh
t,pi
vot
);
quickSort(left,
parti
tionPoint
-1)
;
quickSort(partiti
onPoint+1,
ri
ght)
;
}
}

ADA(
215
0703) 1
5
Pat
elMi
hit 1
800401
07034

i
ntmain()
{
pri
ntf(
"InputArray:"
);
dis
play();
pri
ntl
ine(50);
qui
ckSort(0,MAX-1);
pri
ntf(
"Out putArr
ay:")
;
dis
play();
pri
ntl
ine(50);
}

Output:
InputArray:[4632197]
pivotswapped: 9,7
UpdatedAr ray:
[ 4632179]
pivotswapped: 4,1
UpdatedAr ray:
[ 1632479]
items wapped:6 ,
2
pivotswapped: 6,4
UpdatedAr ray:
[ 1234679]
pivotswapped: 3,3
UpdatedAr ray:
[ 1234679]
OutputAr r
ay:[1234679]

QUE02:Impl
ement
ati
onandTi
meanal
ysi
sofl
inearandbi
nar
ysear
ch
al
gor
ith
m

1
.LinearSear ch
:
//Ccodet olinear
lysear
chxinar
r[]
.Ifx
//i
spr esentthenret
urnit
sloc
ati
on,oth
erwi
se
//ret
ur n-1

ADA(
215
0703) 1
6
Pat
elMi
hit 1
800401
07034

#i
ncl
ude<
stdi
o.h
>

i
nts
earch(
intarr[
],i
ntn,i
ntx
)
{
i
nti;
f
or(i=0;i<n;i+
+)
i
f(arr[
i]==x)
r
eturni;
r
eturn-1;
}

i
ntmain(void)
{
i
ntar r[]={2,3,4,10,
40} ;
i
ntx=1 0;
i
ntn=s izeof
(ar
r)/s i
zeof (arr[0])
;
i
ntr esult=sear
c h
(arr,n,x);
(
r esult==-1) ?pri
nt f
("Elementi snotpres
enti
narray
")
:pri
ntf("
El ementispresentati
ndex%d"
,
result
);
r
et urn0;
}

Output
:
El
ementispr
esentati
ndex3

2.Bi
nar
ySear
ch:
//Cpr
ogr
amt
oimpl
ementr
ecur
siv
eBi
nar
ySear
ch
#i
ncl
ude<
stdi
o.h
>
//Ar
ecur
siv
ebi
nar
ysear
chf
unc
tion.
Itr
etur
ns
//l
ocat
ionofxi
ngi
venar
rayar
r[l
..
r]i
spr
esent
,

ADA(
215
0703) 1
7
Pat
elMi
hit 1
800401
07034

//ot
her
wis
e-1
i
ntbi
nar
ySear
ch(
intar
r[]
,intl
,intr
,intx
)
{
i
f(r>
=l)
{
i
ntmi
d=l+(
r-l
)/2;

//I
fth
eel
ementi
spr
esentatt
hemi
ddl
e
//i
tsel
f
i
f(ar
r[mi
d]=
=x)
r
etur
nmi
d;

//I
fel
ementi
ssmal
lert
hanmi
d,t
hen
//i
tcanonl
ybepr
esenti
nlef
tsubar
ray
i
f(ar
r[mi
d]>x
)
r
etur
nbi
nar
ySear
ch(
arr
,l,
mid-1
,x)
;

//El
set
heel
ementc
anonl
ybepr
esent
//i
nri
ghts
ubar
ray
r
etur
nbi
nar
ySear
ch(
arr
,mi
d+1
,r,
x);
}

//Wer
eac
hher
ewh
enel
ementi
snot
//pr
esenti
nar
ray

ADA(
215
0703) 1
8
Pat
elMi
hit 1
800401
07034

r
etur
n-1
;
}
i
ntmai
n(v
oid)
{
i
ntar
r[]={2,
3,4,
10,
40}
;
i
ntn=s
izeof
(ar
r)/s
izeof
(ar
r[0]
);
i
ntx=1
0;
i
ntr
esul
t=bi
nar
ySear
ch(
arr
,0,
n-1
,x)
;
(
res
ult=
=-1
) ?pr
int
f("
Elementi
snotpr
esenti
nar
ray
")
:
pri
ntf
("El
ementi
spr
esentati
ndex%d"
,
r
esul
t);
r
etur
n0;
}
Out
put:
El
ementi
spr
esentati
ndex3

QUE03:I mpl
ement
ati
onandTi
meanal
ysi
sofKnaps
ackpr
obl
em us
ing
gr
eedypr
ogrammi
ng.

#i
ncl
ude<
stdi
o.h
>
#i
ncl
ude<
coni
o.h>

v
oidknaps ack(i
ntn,f
loatwei
ght
[],
floatpr
ofi
t[]
,fl
oatc
apac
ity
)
{
floatx[20],t
p=0;
inti,j,
u;
u= c
apac i
ty;

ADA(
215
0703) 1
9
Pat
elMi
hit 1
800401
07034

f
or(i=
0;i
<n;
i+
+)
x
[i]
=0.0;

for(
i=0;
i<n;
i++
)
{
if(
weight[
i]>u)
break;
else
{
x[i
]=1.
0;
tp=tp+profi
t[i
];
u=u-weig
h t
[i]
;
}
}

i
f(i
<n)
x
[i]=u/wei
ght
[i]
;

t
p=t
p+(
x[i
]*pr
ofi
t[i
]);

pri
ntf(
"\
nTh eresul
tvec
tori
s:
-")
;
for
(i=
0;i<
n;i+
+)
pri
ntf(
"%ft"
,x[
i])
;

pr
int
f("
mMax
imumpr
ofi
tis
:-%f
",t
p);

v
oidmain(
)
{
f
loatweight
[20],pr
ofi
t[20]
,capac
ity
;
i
ntn,i,j
;
f
loatrati
o[20],
temp;

ADA(
215
0703) 20
Pat
elMi
hit 1
800401
07034

c
lrs
cr(
);

pr
int
f("
nEntert
heno.
ofobj
ect
s:-"
);
sc
anf("
%d"
,&num);

print
f("
nEnterthewtsandprofi
tsofeachobj
ect
:-"
);
for(i
=0;i
<n;
i++)
{
sc
anf(
"%f%f",
&weigh
t[i
],&prof
it[
i])
;
}

pri
ntf(
"nent
erthecapac
ity
aci
tyofknaps
ack:
-"
);
s
canf("
%f"
,&capac
ity
);

f
or(
i=0;i
<n;i
++)
{
r
atio[
i]=
prof
it[
i]/wei
ght
[i]
;
}

f
or(
i=0;i
<n;i
++)
{
for
(j=
i+1;
j<n;j
++)
{
i
f(r
atio[
i]<
rati
o[j]
)
{
temp=ratio[
j];
rati
o[j]
=rati
o[i];
rati
o[i]
=temp;

temp=weig
h t
[j]
;
weig
ht[
j]=weight
[i]
;
weig
ht[
i]=temp;

ADA(
215
0703) 21
Pat
elMi
hit 1
800401
07034

temp=profit
[j]
;
prof
it[
j]=profi
t[i
];
prof
it[
i]=temp;
}
}
}

knapsack(
n,wei
ght
,pr
ofi
t,c
apac
ity
);
getch
();
}

Output:
Entertheno.ofobjects:
-7
Enterthewtsandpr ofi
tsofeachobject:
-
210
35
515
77
16
418
13
Enterthecapacit
yofknaps ack:
-15
Theresultvect
oris:-1
.000000 1 .
000000 1
.000000 1
.000000
1
.000000 0. 66666
7 0. 000000
Max i
mumpr of
iti
s:-55.
333332

QUE04:
Implement
ati
onandTi
meanal
ysi
sofJ
obs
chedul
ingus
ingg
reedy
pr
ogr
amming
.

#inc
lude<
stdi
o.h
>
#defi
neMAX1 00

ADA(
215
0703) 22
Pat
elMi
hit 1
800401
07034

t
ypedefstructJob
{
charid[5];
intdeadline;
intprofit
;
}
J
ob;
v
oidjobSequenc i
ngWi t
hDeadli
ne(
Jobjobs
[],
intn)
;
i
ntminValue(intx,i
nty )
{
if(
x<y )returnx ;
returny;
}
i
ntmain(void)
{
//variables
inti,j
;
//jobswi t
hdeadl ineandpr
ofi
t
Jobjobs[5]=
{
{
"j
1 "
,2,6 0}
,
{
"j
2",1,100},
{
"j
3",3,20} ,
{
"j
4",2,40} ,
{
"j
5 "
,1,20},
};
//temp
Jobtemp;

//numberofj
obs
intn=5;

//s
ortt
hej
obspr
ofi
twi
sei
ndes
cendi
ngor
der

ADA(
215
0703) 23
Pat
elMi
hit 1
800401
07034

for(i=1;i<n; i
++)
{
for(j=0;j<n-i;j++
)
{
i
f(j
obs[j
+1].
profi
t>j obs[
j].prof
it)
{
temp=j obs[j+1]
;
jobs[
j+1]=jobs [
j];
jobs[
j]=temp;
}
}
}
printf
("%10s%10s%10s\
n","J
ob","Deadline",
"Prof
it"
);
for(i=0;i<n; i
++)
{
pr i
ntf
("%10s %10i %1 0i\
n",j obs[i]
.i
d, jobs[
i].
deadl
ine,
jobs[i
].prof
it);
}
jobSequencingWit
hDeadline(jobs,n)
;
return0;
}

v
oidj
obSequencing
With
Deadl
ine(
Jobj
obs
[],
intn)
{
//variabl
es
inti,
j,k,maxprof
it;

//fr
eeti
mes l
ots
intt
imes
lot
[MAX];

//f
ill
edti
meslot
s
i
ntf
il
ledTi
meSl
ot=0;

ADA(
215
0703) 24
Pat
elMi
hit 1
800401
07034

//findmaxdeadl i
nev alue
intdmax=0;
for(i=0;i<n;i
++)
{
if(
jobs[
i].
deadline>dmax )
{
dmax=j obs[i]
.deadl
ine;
}
}

//fr
eetimeslot
sinit
ial
lys
ett
o-1[
-1denot
esEMPTY]
for
(i=1;i<
=dmax ;i
++)
{
t
imesl
ot[i
]=-1;
}

pr
int
f("
dmax
:%d\
n",
dmax
);

f
or(
i=1;i<=n;i++
)
{
k=minValue(dmax ,j
obs[i-1]
.deadl
ine)
;
whi
le(k>=1)
{
if(
timeslot[
k]= =-
1)
{
timeslot[
k]=i -
1;
fil
ledTimeSl
ot +
+;
break;
}
k--
;
}

//i
fal
lti
mes
lot
sar
efi
ll
edt
hens
top

ADA(
215
0703) 25
Pat
elMi
hit 1
800401
07034

if
(fi
lledTi
meSl
ot=
=dmax
)
{
break;
}
}

//requi
r edjobs
pri
ntf("
\nRequiredJobs:"
);
for
(i=1 ;i<=dmax ;i+
+)
{
printf
("%s",
jobs[t
imesl
ot[
i]]
.i
d);
if
(i<dmax )
{
pr i
ntf(
"--
>");
}
}

//requiredprofi
t
max profi
t=0;
for
(i=1 ;i<=dmax ;
i++)
{
max prof
it+=jobs[
timesl
ot[
i]]
.pr
ofi
t;
}
pri
nt f
("\
nMaxPr ofi
t:%d\
n",max
profi
t);
}
Output:
Job Deadl
ine Prof
it
j2 1 1
00
j1 2 60
j4 2 40
j3 3 20
j5 1 20
dmax :
3

ADA(
215
0703) 26
Pat
elMi
hit 1
800401
07034

Requi
redJ
obs:j
2--
>j1-
->j
3
MaxProfi
t:1
80

QUE 05:Implement
ati
on ofmaki
nga c
hang
e pr
obl
em us
ingdy
nami
c
pr
ogr
amming.

#inc
lude< stdi
o.h>
intcount(intS[],i
ntm, intn)
{
inti,j
, x
,y;
//Weneedn+ 1rowsasthetabl
eisc
ons
truc
ted
//inbot tomupmannerus ingth
ebasec
ase0
//v aluecase( n=0)
inttable[n+1]
[m] ;

//Fil
ltheent
eri
esf
or0v
aluec
ase(
n=0)
for(
i=0;
i<m;i
++)
tabl
e[0]
[i]=1
;

//Fil
lrestoft
h etableentr
iesinbottom
//upmanner
for(
i=1 ;
i<n+1;i++
)
{
f
or(j=0;j<m; j+
+)
{
//Countofs ol
utionsinc
ludingS[j]
x=(i-S[j
]>=0)?table[
i-S[j]
] [
j]:
0;

//Countofsol
uti
onsexcl
udi
ngS[
j]
y=(j>=1)
?tabl
e[i
][j
-1]
:0;

//t
otalc
ount

ADA(
215
0703) 27
Pat
elMi
hit 1
800401
07034

t
abl
e[i
][j
]=x+y
;
}
}
r
etur
ntabl
e[n]
[m-
1];
}

//Driverprog r
amt otestabovefuncti
on
intmain()
{
intarr[]={1,
2,3};
intm=s i
zeof
(arr)
/sizeof
(arr[0]
);
intn=4;
print
f("%d",count
(arr,m,n))
;
retur
n0;
}
Output:4

QUE 06 :Implement
ati
on ofAs
sembl
yli
ne s
chedul
ing us
ing dy
nami
c
pr
ogr
ammi ng.

#inc
lude<
stdi
o.h>
#defi
neNUM_ LINE2
#defi
neNUM_ STATI
ON4

//Ut i
lit
yf uncti
ont ofi
ndminimumoft wonumbers
intmin(inta,intb){returna<b?a: b;}
intcarAs s
embl y(
inta[][NUM_ STATI
ON] ,i
ntt[]
[NUM_
STATI
ON]
,int
*
e,i
nt* x)
{
intT1 [
NUM_ STATION],
T2[ NUM_STATION],i
;

T1
[0]=e[
0]+a[
0][
0];
//t
imet
akent
oleav
efi
rsts
tat
ioni
nli
ne
1

ADA(
215
0703) 28
Pat
elMi
hit 1
800401
07034

T2[
0]=e[
1]+a[
1][
0];
//t
imet
akent
oleav
efi
rsts
tat
ioni
nli
ne
2

//FilltablesT1[
]andT2[ ]us i
ngtheabovegiv
enr ecur
siv
e
r
elat
ions
for(
i=1 ;i<NUM_STATION;++i
)
{
T1[
i]=min(
T1[i
-1]+a[
0][i]
,T2[
i-1
]+t[
1][
i]+a[0]
[i]
);
T2[
i]=min(
T2[i
-1]+a[1]
[i]
,T1[
i-1
]+t[
0][
i]+a[1]
[i]
);
}

//Cons
iderex
itt
imesandret
utnmi
nimum
ret
urnmin(
T1[
NUM_ STATI
ON-1]+x
[0],T2[
NUM_
STATI
ON-
1]+
x
[1]
);
}

i
ntmain(
)
{
i
nta[][NUM_STATION]={ {
4,5,3,
2},
{2,10,1,4}}
;
i
ntt[]
[NUM_ STATION]={ {0,7,
4,5},
{0,9,2,8}};
i
nte[]={10,
12},x[
]={ 18,7};

pr
int
f("
%d"
,car
Ass
embl
y(a,
t,e,
x))
;

r
etur
n0;
}
Out
put
:35

ADA(
215
0703) 29
Pat
elMi
hit 1
800401
07034

QUE 07 :Impl
ement
ati
on of a knaps
ack pr
obl
em us
ing dy
nami
c
pr
ogr
amming
.

#i
ncl
ude<
stdi
o.h
>
#i
ncl
ude<
coni
o.h>

v
oidknaps ack(i
ntn,f
loatwei
ght
[],
floatpr
ofi
t[]
,fl
oatc
apac
ity
)
{
floatx[20],t
p=0;
inti,j,
u;
u= c
apac i
ty;

f
or(i=
0;i
<n;
i+
+)
x
[i]
=0.0;

f
or(
i=0;i
<n;i
++)
{
if
(weight[
i]>u)
break;
else
{
x[i
]=1.
0;
tp=tp+profi
t[i
];
u=u-weig
h t
[i]
;
}
}

i
f(i
<n)
x
[i]=u/wei
ght
[i]
;

ADA(
215
0703) 30
Pat
elMi
hit 1
800401
07034

t
p=t
p+(
x[i
]*pr
ofi
t[i
]);

pri
ntf(
"\
nTh eresul
tvec
tori
s:
-")
;
for
(i=
0;i<
n;i+
+)
pri
ntf(
"%ft"
,x[
i])
;

pr
int
f("
mMax
imumpr
ofi
tis
:-%f
",t
p);

}
v
oidmain(
)
{
f
loatweight
[20],pr
ofi
t[20]
,capac
ity
;
i
ntn,i,j
;
f
loatrati
o[20],
temp;
c
lrscr
();

pr
int
f("
nEntert
heno.
ofobj
ect
s:-"
);
sc
anf("
%d"
,&num);

print
f("
nEnterthewtsandprofi
tsofeachobj
ect
:-"
);
for(i
=0;i
<n;
i++)
{
sc
anf(
"%f%f",
&weigh
t[i
],&prof
it[
i])
;
}

pr
int
f("
nenterthecapac
ity
aci
tyofknaps
ack:
-"
);
sc
anf("
%f"
,&capaci
ty)
;

f
or(
i=0;i
<n;i
++)
{
r
atio[
i]=
prof
it[
i]/wei
ght
[i]
;
}

ADA(
215
0703) 31
Pat
elMi
hit 1
800401
07034

f
or(
i=0;i
<n;i
++)
{
for
(j=
i+1;
j<n;j
++)
{
i
f(r
atio[
i]<
rati
o[j]
)
{
temp=ratio[
j];
rati
o[j]
=rati
o[i];
rati
o[i]
=temp;

temp=weig
h t
[j]
;
weig
ht[
j]=weight
[i]
;
weig
ht[
i]=temp;

temp=profit
[j]
;
prof
it[
j]=profi
t[i
];
prof
it[
i]=temp;
}
}
}

knapsack(
n,weigh
t, pr
ofi
t,capac
ity
) ;
getch(
);
}
Output:
Ent
ertheno.ofobj
ec ts
:-7
Ent
erthewtsandpr ofi
tsofeachobject:
-
210
35
515
77
16
418

ADA(
215
0703) 32
Pat
elMi
hit 1
800401
07034

13
Enterthecapacit
yofknaps ack:
-15
Theresultvect
oris:-1
.000000 1 .
000000 1
.000000 1
.000000
1
.000000 0. 66666
7 0. 000000
Max i
mumpr of
iti
s:-55.
333332

QUE08:Impl
ement
ati
onofc
hai
nmat
rixmul
ti
pli
cat
ionus
ingdy
nami
c
pr
ogr
ammi
ng.

#i
ncl
ude<
stdi
o.h
>
#i
ncl
ude<
limi
ts.
h>

//Matri
xAihasdimensionp[
i-1
]xp[i
]fori=1
..
n
intMat
rix
Chai
nOrder(i
ntp[],
intn)
{

/*Forsimpli
c i
tyoft
heprog
ram,oneex
trarowandoneext
ra
col
umnar eall
ocatedinm[][
].0t
hrowand0thcol
umnofm[][
]are
notus
ed* /
intm[n]
[n];

i
nti
,j,
k,L,
q;

/*m[i
,j
]=Mini
mumnumberofscalarmult
ipl
icat
ionsneededto
computethemat
rixA[i
]A[
i+1
]..
.
A[j
]=A[i
.
.j]wh
eredimensionofA[i
]is
p[i
-1]xp[i
]*/

//costiszer
owh enmul
ti
ply
ingonemat
rix
.
for(
i=1;i
<n;i
++)
m[i]
[i]=0;

//Lischai
nlength
.
for(
L=2;
L<n;L++
)

ADA(
215
0703) 33
Pat
elMi
hit 1
800401
07034

{
f
or(
i=1;i<
n-L+1;i+
+)
{
j=i+L-
1 ;
m[i][j
]=I NT_MAX;
for(k=i;
k<=j-
1;k++
)
{
//q=c ost/scal
armulti
pli
cati
ons
q=m[ i][
k]+m[ k+1
][j
]+p[i-
1]*
p[k]
*p[
j];
if(q<m[ i
][j
])
m[ i
][j
]=q;
}
}
}
r
etur
nm[
1][
n-1
];
}
intmain()
{
intarr[
]={ 1,2,
3,4};
intsiz
e=s i
zeof(
arr)
/si
zeof(
arr[
0]);
print
f("
Mi nimumnumberofmultipl
icat
ionsi
s%d"
,
Mat ri
xChainOrder(
arr,
siz
e));

g
etchar
();
r
etur
n0;
}
Out
put
:Mi
nimumnumberofmul
ti
pli
cat
ionsi
s18

QUE09:
Impl
ementl
ong
estc
ommons
ubs
equenc
e(LCS)pr
obl
em.

#incl
ude< bi
ts/stdc
++.
h>
intmax(inta,intb);
/*Returnsleng t
hofLCSforX[
0..
m-1
],Y[
0..
n-
1]*
/

ADA(
215
0703) 34
Pat
elMi
hit 1
800401
07034

i
ntl
cs(char*X,
char*Y,
intm,
intn)
{
i
ntL[m+1][
n+1]
;
i
nti,j
;

/*Foll
owings tepsbui l
dL[ m+1
][n+
1]inbott
omupfashi
on.Not
e
thatL[i
][j
]c ontainslengthofLCSofX[0.
.
i-
1]andY[
0..
j
-1]*
/
for(i
=0;i<
=m; i++)
{
f
or( j
=0;j<=n;j+
+ )
{
if(i==0||j= =0)
L[i][
j]=0;

el
seif(X[
i-1
]= =Y[j
-1]
)
L[
i][
j]=L[i
-1][
j-1
]+1 ;

el
se
L[
i][
j]=max
(L[
i-1
][j
],L[
i][
j-1
]);
}
}

/*L[m][
n]cont
ainsl
eng
thofLCSf
orX[
0..
n-
1]andY[
0..
m-1
]*/
ret
urnL[m]
[n];
}

/*Util
it
yfunc
tiont
ogetmaxof2i
nteg
ers*
/
intmax(i
nta,i
ntb)
{
r
eturn(
a>b)?a:b;
}

/*Dr
iverpr
ogr
amt
otes
tabov
efunc
tion*
/

ADA(
215
0703) 35
Pat
elMi
hit 1
800401
07034

i
ntmain()
{
c
h arX[
]="
AGGTAB";
c
h arY[
]="
GXTXAYB"
;

i
ntm=str
len(
X);
i
ntn=s
trl
en(Y);

pr
int
f("
Leng
thofLCSi
s%d"
,lc
s(X,
Y,m,
n))
;

r
etur
n0;
}

Output:
Lengt
hofLCSi
s4

QUE1
0:I
mpl
ement
ati
onofRabi
nKar
ppr
obl
emofs
tri
ngmat
chi
ng.

#i
ncl
ude<
stdi
o.h
>
#i
ncl
ude<
stri
ng.
h>

//disth
enumberofc
har
act
ersi
nth
einputal
phabet
#def
ined25
6

/*pat->pattern
txt->text
q- >Apr i
menumber
*/
voi
dsear c
h(charpat[],
chart
xt[
],i
ntq)
{
intM =s tr
len(pat
);
intN=s t
rlen(
txt)
;

ADA(
215
0703) 36
Pat
elMi
hit 1
800401
07034

i
nti
,j;
i
ntp=0;//has
hvaluef
orpat
ter
n
i
ntt=0;//h
ashval
uefort
xt
i
nth=1;

//Theval
ueofhwouldbe"
pow(
d,M-
1)%q"
for(
i=0;i<M-1;
i++
)
h=(
h*d)
%q;

//Calcul
atethehashvalueofpat
ter
nandf
irs
t
//windowoftext
for(
i=0;i<M; i
++)
{
p=(d*p+pat[i
])%q;
t=(d*
t+txt[
i])
%q;
}

//Sli
det
hepat
ter
novert
extonebyone
for(
i=0;
i<=N-M;i+
+)
{

//Chec kth
eh ashvaluesofc urrentwi
ndowoft
ext
//andpat tern.Iftheh ashvaluesmatchth
enonl
y
//chec kf
orc haractersonbyone
if(p==t)
{
/*Chec kforc haracter
sonebyone* /
for(
j=0; j<M; j++)
{
if(txt[i
+j]!=pat[j
] )
break;
}

ADA(
215
0703) 37
Pat
elMi
hit 1
800401
07034

//ifp==tandpat[
0..
.
M-1]=t
xt[i,i
+1,
..
.i
+M-1]
if(
j==M)
pri
ntf
("Pat
ter
nfoundatindex%d\n",
i);
}

//Calculat
eh ashval
uefornextwindowoft
ext
:Remov
e
//leadingdig
it,
addtrai
lingdig
it
if(i<N-M)
{
t=(d*(
t-txt[
i]*
h)+txt[
i+M])%q;

//Wemightgetneg
ati
vev
alueoft
,conv
ert
ingi
t
//toposi
ti
ve
if(
t<0)
t=(t+q)
;
}
}
}

/*Driv
erpr ogramt otestabov
efunc
tion*
/
intmain()
{
c
h artxt
[]=" GEEKSFORGEEKS";
c
h arpat[]="GEEK";
i
ntq=1 01 ;
//Apr imenumber
s
ear ch(
pat ,t
xt,q)
;
r
et ur
n0;
}

Out
put:
Pat
ter
nfoundati
ndex0
Pat
ter
nfoundati
ndex1
0

ADA(
215
0703) 38
Pat
elMi
hit 1
800401
07034

QUE1
1:I
mpl
ement
ati
onofKnut
h-Mor
ris
-Pr
attpr
obl
emofs
tri
ngmat
chi
ng.
#i
ncl
ude<
stdi
o.h
>
#i
ncl
ude<
str
ing
.h>
#def
inec25
6
v
oids
ear
ch(
charpat
ter
n[]
,ch
art
ext
[])
{
i
ntM =s
trl
en(
pat
ter
n);
i
ntN=s
trl
en(
tex
t);
i
nti
,j;
i
nth
ash
P=0;
i
nth
ash
T=0;
i
nth=1
;
f
or(
i=0;
i<M -1
;i+
+)
h=(
h*c
)%1
03;
f
or(
i=0;
i<M;
i++
)
{
h
ash
P=(
c*h
ash
P+pat
ter
n[i
])%1
03;
h
ash
T=(
c*h
ash
T+t
ext
[i]
)%1
03;
}
f
or(
i=0;
i<=N-M;
i++
)
{
i
f(h
ash
P==h
ash
T)
{
f
or(
j=0;
j<M;
j++
)

ADA(
215
0703) 39
Pat
elMi
hit 1
800401
07034

{
i
f(t
ext
[i+j
]!=pat
ter
n[j
])
br
eak;
}
i
f(j=
=M)
pr
int
f("
Pat
ter
nfoundati
ndex%d\
n",
i);
}
i
f(i<N-M)
{
h
ash
T=(
c*(
has
hT-t
ext
[i]*h
)+t
ext
[i+M]
)%1
03;
i
f(h
ash
T<0)
h
ash
T=(
has
hT+1
03)
;
}
}
}
i
ntmai
n()
{
c
hart
ext
[]="
xyz
trwqx
yzf
g";
c
harpat
ter
n[]="
xyz
";
pr
int
f("
Thepat
ter
nisf
oundi
nth
etex
tatt
hef
oll
owi
ngi
ndex:
\
n")
;
s
ear
ch(
pat
ter
n,t
ext
);
r
etur
n0;

}
Out
put
:

ADA(
215
0703) 40
Pat
elMi
hit 1
800401
07034

Th
epat
ter
nisf
oundi
nth
etex
tatt
hef
oll
owi
ngi
ndex−

Pat
ter
nfoundati
ndex0
Pat
ter
nfoundati
ndex7

ADA(
215
0703) 41

You might also like