You are on page 1of 61

ALGORITHMS

INDEX

S.No.


PROGRAMS

Remarks
1.
WAP to implement Assembly Line
Scheduling

2.
WAP to Iind the Longest Common
Subsequence

3.
WAP to implement Matrix Chain
Multiplication

4.
WAP to implement the Recursive
Activity Selector

5.
WAP to implement the Iterative
Activity Selector

6.
WAP to implement Insertion Sort
algorithm

7.
WAP to implement Heap Sort
algorithm

8.
WAP to implement Quick Sort
algorithm

9.
WAP to implement Randomized
Quick Sort
algorithm

10.
WAP to implement Radix sort
algorithm

11.
WAP to implement Bucket sort
algorithm

12.
WAP to implement Counting Sort
algorithm

13.
WAP to implement Priority Queue
(using
maxheap)










PROGRAM 1
Assembly Line Scheduling


//Program to implement Assembly Line Scheduling algorithm
#includeiostream.h~
#includeconio.h~
#deIine numstn 6
#deIine numlines 2
void cost(int a|||numstn|);
void trItime(int t|||numstn-1|);
void entry(int e||);
void exit(int x||);
void Iastestway(int a|||numstn|,int t|||numstn-1|,int e||,int x||,int n,int I|||numstn|,int l|||numstn|,int *I2,int
*l2);
void printstn(int l|||numstn|,int n,int l1);
void main()

clrscr();
int I|numlines||numstn|;
int l|numlines||numstn|;
int a|numlines||numstn|;
int t|numlines||numstn-1|;
int e|numlines|;
int x|numlines|;
int nnumstn;
int I1,l1;
cost(a);
trItime(t);
entry(e);
exit(x);
Iastestway(a,t,e,x,n,I,l,&I1,&l1);
printstn(l,n,l1);
Ior(int i0;inumlines;i)

cout"\n";
Ior(int j0;jnumstn;j)

coutI|i||j|"\t";
}
}
cout"\n";
Ior(int p0;pnumlines;p)

cout"\n";
Ior(int q0;qnumstn-1;q)

coutl|p||q|"\t";
}
}
getch();
}
void cost(int a|||numstn|)

Ior(int i0;inumlines;i)

Ior(int j0;jnumstn;j)

cout"Enter the cost oI ("i1","j1") : ";
cin~~a|i||j|;
}
}
}
void trItime(int t|||numstn-1|)

Ior(int i0;inumlines;i)

Ior(int j0;jnumstn-1;j)

iI(i0)

cout"Enter the transIer time between ("i1","j1") and
("i2","j2") : ";
cin~~t|i||j|;
}
else

cout"Enter the transIer time between ("i1","j1") and
("i","j2") : ";
cin~~t|i||j|;
}
}
}
}
void entry(int e||)

Ior(int i0;inumlines;i)

cout"Enter entry time oI assembly line "i1" : ";
cin~~e|i|;
}
}
void exit(int x||)

Ior(int i0;inumlines;i)

cout"Enter exit time oI assembly line "i1" : ";
cin~~x|i|;
}
}
void Iastestway(int a|||numstn|,int t|||numstn-1|,int e||,int x||,int n,int I|||numstn|,int l|||numstn|,int *I2,int
*l2)

I|0||0|e|0|a|0||0|;
I|1||0|e|1|a|1||0|;
Ior(int z1;zn;z)

iI(I|0||z-1|a|0||z|I|1||z-1|t|1||z-1|a|0||z|)

I|0||z|I|0||z-1|a|0||z|;
l|0||z-1|1;
}
else

I|0||z|I|1||z-1|t|1||z-1|a|0||z|;
l|0||z-1|2;
}
iI(I|1||z-1|a|1||z|I|0||z-1|t|0||z-1|a|1||z|)

I|1||z|I|1||z-1|a|1||z|;
l|1||z-1|2;
}
else

I|1||z|I|0||z-1|t|0||z-1|a|1||z|;
l|1||z-1|1;
}
}
iI(I|0||n-1|x|0|I|1||n-1|x|1|)

*I2I|0||n-1|x|0|;
*l20;
}
else

*I2I|1||n-1|x|1|;
*l21;
}
}
void printstn(int l|||numstn|,int n,int l1)

int il1;
cout"\nline "i1" station "nendl;
Ior(int bn-2;b~0;b--)

il|i||b|;
cout"line "i" station "b1endl;
ii-1;
}
}

OUTPUT


Enter the cost oI (1,1) : 7
Enter the cost oI (1,2) : 9
Enter the cost oI (1,3) : 3
Enter the cost oI (1,4) : 4
Enter the cost oI (1,5) : 8
Enter the cost oI (1,6) : 4
Enter the cost oI (2,1) : 8
Enter the cost oI (2,2) : 5
Enter the cost oI (2,3) : 6
Enter the cost oI (2,4) : 4
Enter the cost oI (2,5) : 5
Enter the cost oI (2,6) : 7
Enter the transIer time between (1,1) and (2,2) : 2
Enter the transIer time between (1,2) and (2,3) : 3
Enter the transIer time between (1,3) and (2,4) : 1
Enter the transIer time between (1,4) and (2,5) : 3
Enter the transIer time between (1,5) and (2,6) : 4
Enter the transIer time between (2,1) and (1,2) : 2
Enter the transIer time between (2,2) and (1,3) : 1
Enter the transIer time between (2,3) and (1,4) : 2
Enter the transIer time between (2,4) and (1,5) : 2
Enter the transIer time between (2,5) and (1,6) : 1
Enter entry time oI assembly line 1 : 2
Enter entry time oI assembly line 2 : 4
Enter exit time oI assembly line 1 : 3
Enter exit time oI assembly line 2 : 2

line 1 station 6
line 2 station 5
line 2 station 4
line 1 station 3
line 2 station 2
line 1 station 1

9 18 20 24 32 35
12 16 22 25 30 37

1 2 1 1 2
1 2 1 2 2









PROGRAM 2
Longest Common Subsequence


//Program to implement longest common subsequence algorithm
#includeiostream.h~
#includeconio.h~
#includestring.h~

void printlcs(char b|20||20|,char x||,int i,int j);
void lcs(char x||,char y||)

int mstrlen(x);
int nstrlen(y);
char c|20||20|;
char b|20||20|;
Ior(int i1;im;i)
c|i||0|0;
Ior(int j0;jm;j)
c|0||j|0;
Ior(i1;im;i)

Ior(j1;jn;j)

iI(x|i|y|i|)

c|i||j|c|i-1||j-1|1;
b|i||j|'/';
}
else iI(c|i-1||j|~c|i||j-1|)

c|i||j|c|i-1||j|;
b|i||j|',';
}
else

c|i||j|c|i||j-1|;
b|i||j|'-';
}
}
}
cout"\n The longest common sequence Iound is: "endl;
printlcs(b,x,m,n);
}

void printlcs(char b|20||20|, char x||, int i, int j)

iI((i0),,(j0))
return;
iI(b|i||j|'/')

printlcs(b,x,i-1,j-1);
coutx|i|" ";
}
else iI(b|i||j|',')
printlcs(b,x,i-1,j);
else
printlcs(b,x,i,j-1);
}

void main()

clrscr();
char x|20|,X|20|,y|20|,Y|20|;
cout"\n Enter Iirst sequence\t"endl;
cin~~x;
cout"\n Enter second sequence: "endl;
cin~~y;
Ior(int i0;istrlen(x);i)
X|i1|x|i|;
Ior(i0;istrlen(y);i)
Y|i1|y|i|;
lcs(X,Y);
getch();
}





OUTPUT



Enter Iirst sequence
abscI
Enter second sequence:
abodrsc
The longest common sequence Iound is:
absc









PROGRAM 3
Matrix Chain Multiplication


//Program to implement matrix chain mutiplication algorithm
#includeiostream.h~
#includeconio.h~

void main()

clrscr();
int a|20||20|,p|10|,n,i,j,m|20||20|,q;
cout"Enter the no. oI matrices: "endl;
cin~~n;
Ior(i1;in;i)

cout"Enter the dimension oI "i"matrix"endl;
Ior(j1;j2;j)

cin~~a|i||j|;
}
}
Ior(i1;in1;i)

p|0|a|1||1|;
p|i|a|i||2|;
}
Ior(i1;in;i)

m|i||i|0;
}
Ior(int l2;ln;l)

Ior(i1;in-l1;i)

ji l-1;
m|i||j|175751;
Ior(int ki;kj-1;k)

qm|i||k|m|k1||j|(p|i-1|*p|k|*p|j|);
iI(qm|i||j| && q~0)

m|i||j|q;
}
}
}
}



cout"The matrix is:\n"endl;
Ior(i1;in;i)

Ior(j1;jn;j)

iI(ij)
coutm|i||j|" ";
}
coutendl;
}
getch();
}


OUTPUT


Enter the no. oI matrices:
5
Enter the dimension oI 1matrix
2
3
Enter the dimension oI 2matrix
3
4
Enter the dimension oI 3matrix
4
5
Enter the dimension oI 4matrix
5
6
Enter the dimension oI 5matrix
6
7
The matrix is:

0 24 64 124 208
0 60 150 276
0 120 288
0 210
0








PROGRAM 4
Recursive Activity Selector


//Program to implement Recursive activity selector algorithm
#includeiostream.h~
#includeconio.h~
#deIine MAX 10000

void startingtime(int n, int s|100|);
void Iinishingtime(int n, int I|100|);
void recursiveactivityselector(int s|100|, int I|100|, int i, int j);

void main()

clrscr();
int n, s|100|, I|100|;
cout"\n\n Enter number oI activities : ";
cin~~n;
startingtime(n, s);
Iinishingtime(n, I);
recursiveactivityselector(s, I, 0, n1);
getch();
}

void startingtime(int n, int s|100|)

cout"\n\n Enter starting times oI activities : ";
s|0|0;
Ior(int i1; in; i)
cin~~s|i|;
s|n1|MAX;
}

void Iinishingtime(int n, int I|100|)

cout"\n\n Enter Iinishing times oI activities : ";
I|0|0;
Ior(int i1; in; i)
cin~~I|i|;
I|n1|MAX;
cout"\n\n Compatible activities are : \n";
}

void recursiveactivityselector(int s|100|, int I|100|, int i, int j)

int mi1;
while((mj)&&(s|m|I|i|))
mm1;
iI(mj)

cout"A|"m"|\t";
recursiveactivityselector(s, I, m, j);
}
}




OUTPUT



Enter number oI activities : 11
Enter starting times oI activities : 1 3 0 5 3 5 6 8 8 2 12
Enter Iinishing times oI activities : 4 5 6 7 8 9 10 11 12 13 14
Compatible activities are :
A|1| A|4| A|8| A|11|
































PROGRAM 5
Iterative Activity Selector


//Program to implement Iterative activity selector algorithm
#includeiostream.h~
#includeconio.h~
#deIine MAX 10000

void startingtime(int n, int s|100|);
void Iinishingtime(int n, int I|100|);
void iterativeactivityselector(int s|100|, int I|100|, int n);

void main()

clrscr();
int n, s|100|, I|100|;
cout"\n\n Enter number oI activities : ";
cin~~n;
startingtime(n, s);
Iinishingtime(n, I);
iterativeactivityselector(s, I, n);
getch();
}

void startingtime(int n, int s|100|)

cout"\n\n Enter starting times oI activities : ";
s|0|0;
Ior(int i1; in; i)
cin~~s|i|;
s|n1|MAX;
}

void Iinishingtime(int n, int I|100|)

cout"\n\n Enter Iinishing times oI activities : ";
I|0|0;
Ior(int i1; in; i)
cin~~I|i|;
I|n1|MAX;
}

void iterativeactivityselector(int s|100|, int I|100|, int n)

cout"\n\n Compatible activities are : \n";
int i1;
cout"\n A|"i"|\t";
Ior(int m2; mn; m)

iI(s|m|~I|i|)

cout"A|"m"|\t";
im;
}
}
}




OUTPUT



Enter number oI activities : 11
Enter starting times oI activities : 1 3 0 5 3 5 6 8 8 2 12
Enter Iinishing times oI activities : 4 5 6 7 8 9 10 11 12 13 14
Compatible activities are :
A|1| A|4| A|8| A|11|





























PROGRAM 6
Insertion Sort

//Program to implement insertion sort algorithm
#includeiostream.h~
#includeconio.h~
void input(int arr|50|,int sz)

Ior(int i0;isz;i)

cout"Enter element "(i1)" : ";
cin~~arr|i|;
}
}
void insertion(int arr|50|,int sz)

int temp;
Ior(int i1;isz;i)

Ior(int j0;ji;j)

iI(arr|j|~arr|i|)

temparr|j|;
arr|j|arr|i|;
Ior(int ki;k~j;k--)
arr|k|arr|k-1|;
arr|k1|temp;
}
}
}
}
void display(int arr|50|,int sz)

cout"Sorted Array :\n";
Ior(int i0;isz;i)
coutarr|i|" ";
}
void main()

clrscr();
int arr|50|,size;
cout"Enter the size oI the array : ";
cin~~size;
input(arr,size);
insertion(arr,size);
display(arr,size);
getch();
}
OUTPUT


Enter the size oI the array : 7
Enter element 1 : 0
Enter element 2 : 4
Enter element 3 : 2
Enter element 4 : 7
Enter element 5 : 8
Enter element 6 : 3
Enter element 7 : 4
Sorted Array :
0 2 3 4 4 7 8





































PROGRAM 7
Heap Sort


//Program to implement Heapsort algorithm
#includeiostream.h~
#includeconio.h~
#includestdio.h~

void maxheapiIy(int *a,int i);
void buildmaxheap(int *a);
void heapsort(int *a);

int n,size;

void main()



clrscr();

int a|100|;
cout"Enter the size oI the heap : "endl;
cin~~n;
sizen;
cout"Enter the values oI the heap : "endl;
Ior(int i1;in;i)
cin~~a|i|;
heapsort(a);

getch();

}

void buildmaxheap(int *a)


Ior(int in/2;i~1;i--)

maxheapiIy(a,i);
}

}



void maxheapiIy(int *a,int i)


int l2*i;
int r2*i1;
int largest;
iI(ln && a|l|~a|i|)
largestl;
else
largesti;
iI(rn && a|r|~a|largest|)
largestr;

iI(largest!i)

int tempa|i|;
a|i|a|largest|;
a|largest|temp;
maxheapiIy(a,largest);
}

}

void heapsort(int *a)


buildmaxheap(a);
Ior(int in;i~2;i--)

int tempa|1|;
a|1|a|i|;
a|i|temp;
nn-1;
maxheapiIy(a,1);
}
cout"The sorted array is :"endl;
Ior(i1;isize;i)
couta|i|" ";

}

OUTPUT

Enter the size oI the heap :
6
Enter the values oI the heap :
31
72
50
19
12
3
the sorted array is :
3 12 19 31 50 72
PROGRAM 8
Quick Sort


//Program to implement Quicksort algorithm
#includeiostream.h~
#includeconio.h~
#includestdio.h~

void quick(int *,int p,int r);
int partition(int *,int p,int r);

void main()

clrscr();
int a|100|,s;
cout"Enter the no oI elements oI the array : "endl;
cin~~s;
cout"Enter the values"endl;
Ior(int i0;is;i)
cin~~a|i|;
quick(a,0,s-1);
cout"The sorted array is"endl;
Ior(i0;is;i)
couta|i|" ";
getch();
}

void quick(int *a,int p,int r)

iI(pr)

int qpartition(a,p,r);
quick(a,p,q-1);
quick(a,q1,r);
}
}

int partition(int *a,int p,int r)

int xa|r|,temp;
int ip-1;




Ior(int jp;jr-1;j)

iI(a|j|x)

ii1;
tempa|i|;
a|i|a|j|;
a|j|temp;
}
}
tempa|i1|;
a|i1|a|r|;
a|r|temp;
return i1;
}






OUTPUT



Enter the no oI elements oI the array :
6
Enter the values
23
12
10
15
56
36
The sorted array is
10 12 15 23 36 56
















PROGRAM 9
Randomized Quick Sort


//Program to implement randomized quicksort algorithm
#includestdio.h~
#includestdlib.h~
#includeiostream.h~
#includeconio.h~
int partition(int a||,int l,int r)

int temp;
int il-1;
int j;
randomize();
int prandom(r-l);
ppl;
tempa|p|;
a|p|a|r|;
a|r|temp;
int va|r|;
Ior(jl;jr-1;j)

iI(a|j|v)

ii1;
tempa|i|;
a|i|a|j|;
a|j|temp;
}
}
tempa|i1|;
a|i1|a|r|;
a|r|temp;
return i1;
}
void quicksort(int a||,int l,int r)

iI(lr)

int ipartition(a,l,r);
quicksort(a,l,i-1);
quicksort(a,i1,r);
}
}
void main()

clrscr();
int a|20|,n,i;
Ior(i1;i20;i)
a|i|0;
cout"\nEnter the no.oI elements";
cin~~n;
cout"\nEnter the elements";
Ior(i1;in;i)
cin~~a|i|;
cout"\nSorted array is:\n";
quicksort(a,1,n);
Ior(i1;in;i)
couta|i| ';
getch();
}






OUTPUT



Enter the no oI elements5

Enter the elements6
4
2
7
9
Sorted array is:
2 4 6 7 9


















PROGRAM 10
Radix Sort

//Program to implement Radix sort algorithm
#includeiostream.h~
#includeconio.h~
#includemath.h~
#includestdio.h~

void radixsort(int x||,int n,int m)

int i,j,k,c,d,Ilag,exp;
Ior(i1;im;i)

exppow(10,i-1);
Ior(j0;jn-1;j)

Ior(k0;kn-j-1;k)

c(x|k|/exp)10;
d(x|k1|/exp)10;
iI(c~d)

Ilagx|k|;
x|k|x|k1|;
x|k1|Ilag;
}
}
}
}
}

void main()

clrscr();
int a|20|,n,s,i;
cout"Enter the no. oI digits: "endl;
cin~~s;
cout"Enter the no. oI elements in the array: "endl;
cin~~n;
cout"Enter the elements: "endl;
Ior(i0;in;i)
cin~~a|i|;
radixsort(a,n,s);
cout"The sorted order: "endl;
Ior(i0;in;i)
couta|i|"\n";
getch();
}
OUTPUT



Enter the no oI elements oI the array :
5
Enter the no oI digits oI the nos :
3
Enter the elements oI the array.
450
230
540
125
100
The sorted array is
100 125 230 450 540

































PROGRAM 11
ucket Sort


//Program to implement Bucket sort algorithm
#includeiostream.h~
#includeconio.h~
#includestdio.h~
class node

private:
int count;
Iloat num;
node *ptr,*Iirst;
public:
node();
void create(Iloat a);
void display();
};

node::node()

IirstNULL;
}
void node::create(Iloat a)

node *head,*temp,*x;
headnew node;
head-~numa;
iI(IirstNULL)

head-~ptrNULL;
Iirsttemphead;
}
else

xtempIirst;

while(temp-~numa && temp!NULL)

xtemp;
temptemp-~ptr;
}



iI(tempIirst)

head-~ptrIirst;
Iirsthead;
}
else

x-~ptrhead;
head-~ptrtemp;
}
}
count;
}

void node::display()

node *temp;
tempIirst;
while(temp!NULL)

cout" "temp-~num;
temptemp-~ptr;
}

}
void main()

clrscr();
Iloat a|100|;
int n;
cout" Enter the no oI elements : ";
cin~~n;
node z|10|;
cout"enter the elements........"endl;
Ior(int i0;in;i)
cin~~a|i|;
Ior(int g0;g10;g)

Iloat d(Iloat)g/10;
Ior(int j0;jn;j)

iI(a|j|~d && a|j|(Iloat)d0.1)

z|g|.create(a|j|);
}
}
}
cout"the sorted array is......"endl;
Ior(i0;i10;i)
z|i|.display();
getch();
}


OUTPUT



Enter the no oI elements :
5
Enter the elements
0.54
0.23
0.54
0.12
0.10
The sorted array is
0.1 0.12 0.23 0.54 0.54




































PROGRAM 12
Counting Sort



//Program to implement Counting sort algorithm
#includeiostream.h~
#includeconio.h~
#includestdio.h~

void main()

clrscr();
int a|100|,b|100|,c|100|;
int n,max,i,j,k,count0;
cout"Enter the no oI elements oI the array : "endl;
cin~~n;
cout"Enter the nos."endl;
Ior(i1;in;i)
cin~~a|i|;
cout"Enter the max no: "endl;
cin~~max;
Ior(i0;imax;i)

count0;
Ior(j1;jn;j)

iI(ia|j|)
count;
}
b|i|count;
}

Ior(i1;imax;i)
b|i|b|i-1|b|i|;
Ior(in;i~0;i--)

Ior(j0;jmax;j)

iI(a|i|j)

c|b|j||j;
b|j|--;
}
}
}

cout"The sorted array is......."endl;
Ior(i1;in;i)
coutc|i|" ";
getch();
}







OUTPUT



Enter the no oI elements oI the array :
5
Enter the nos
2
6
4
7
9
Enter the max no:
9
The sorted array is
2 4 6 7 9

























PROGRAM 13
Priority Queue


//Program to implement priority queues (using max heap)
#includeiostream.h~
#includeconio.h~

class priority

int heapsize, largest, a|50|, count;
public:
priority()

count0;
heapsize0;
}
void swap(int &a, int &b);
int heapextractmax();
int parent(int);
int leIt(int);
int right(int);
int heapmax();
void heapiIy(int);
void heapincreasekey(int, int);
void maxheapinsert(int);
void getarray();
void getcount();
};

int priority::heapextractmax()

iI(heapsize1)

cout"\n\n Heap UnderIlow!!!";
return 0;
}
else

int maxa|1|;
a|1|a|heapsize|;
heapsize-1;
heapiIy(1);
return max;
}
}

void priority::heapiIy(int i)

int lleIt(i);
int rright(i);
int largesti;
iI(lheapsize)

count;
iI(a|l|~a|i|)
largestl;
else
largesti;
}
iI(rheapsize)

count;
iI(a|r|~a|largest|)
largestr;
}
iI(largest!i)

swap(a|largest|, a|i|);
heapiIy(largest);
}
}

void priority::swap(int &a, int &b)

int temp0;
tempa;
ab;
btemp;
}

void priority::heapincreasekey(int i, int key)

iI(keya|i|)

cout"\n\n New key smaller than current key!!!";
count;
}
else

a|i|key;
while((i~1)&&(a|parent(i)|a|i|))

count;
int tempa|i|;
a|i|a|parent(i)|;
a|parent(i)|temp;
iparent(i);
}
}
}

void priority::maxheapinsert(int key)

heapsize;
a|heapsize|-3000;
heapincreasekey(heapsize, key);
}

void priority::getarray()

cout"\n\n The resultant array is : ";
Ior(int i1; iheapsize; i)
cout" "a|i|;
}

void priority::getcount()

cout"\n\n The number oI comparisons are : "count;
}

int priority::parent(int i)

return (i/2);
}

int priority::leIt(int i)

return (2*i);
}

int priority::right(int i)

return (2*i1);
}

void main()

clrscr();
int num;
int ch;
char ch1;
priority ob;
do

cout"\n\n 1. Insert an element"
"\n 2. Display array"
"\n 3. Extract maximum element"
"\n\n Enter your choice : ";
cin~~ch;
switch(ch)

case 1 : cout"\n\n Enter the value : ";
cin~~num;
ob.maxheapinsert(num);
break;
case 2 : ob.getarray();
ob.getcount();
break;
case 3 : cout"Maximum element in array is : "ob.heapextractmax();
break;
deIault : cout"\n\n Wrong choice!! Try again...!";
}
cout"\n\n Do you want to continue?";
cin~~ch1;
}while((ch1'y'),,(ch1'Y'));
getch();
}






OUTPUT




1. Insert an element
2. Display array
3. Extract maximum element

Enter your choice : 1

Enter the value : 5

Do you want to continue?y

1. Insert an element
2. Display array
3. Extract maximum element

Enter your choice : 1

Enter the value : 6

Do you want to continue?y

Insert an element
Display array
Extract maximum element

Enter your choice : 1

Enter the value : 7

Do you want to continue?y

Insert an element
Display array
Extract maximum element

Enter your choice : 2

The resultant array is : 7 5 6
The number oI comparisons are : 2

Do you want to continue?y

Insert an element
Display array
Extract maximum element

Enter your choice : 3
Maximum element in array is : 7

Do you want to continue?y

Insert an element
Display array
Extract maximum element

Enter your choice : 2

The resultant array is : 6 5

The number oI comparisons are : 3

Do you want to continue?n

You might also like