Sie sind auf Seite 1von 18

/*

NAME-Himanshu Mundeepi
CLASS-B.Sc(h) Comp. Sci
ROLL NO.-7013

Q-WAP to sort a no.'s using count sort.


*/

#include<iostream.h>
#include<conio.h>

int a[50],b[50],c[50];
int n;
int ct;
void count()
{
ct=0;
}
void set(int ar[],int p);
int* get();
void countsort();

void set(int ar[],int p)


{
n=p;
for(int i=1;i<=n;i++)
a[i]=ar[i];
}
void countsort()
{
int i,j;
int k=a[1];
for(i=2;i<=n;i++)
{
if(k<a[i])
k=a[i];
}
for(i=0;i<=k;i++)
{
c[i]=0;
ct++;

}
for(j=1;j<=n;j++)
{
c[a[j]]=c[a[j]]+1;
ct++;

}
for(i=1;i<=k;i++)
{
c[i]=c[i]+c[i-1];
ct++;
}
for(j=n;j>=1;j--)
{
b[c[a[j]]]=a[j];
c[a[j]]=c[a[j]]-1;
ct++;
}

}
int* get()
{
return &(b[1]);
}

void main()
{
clrscr();
int n,i;
int ar[50];
cout<<"ENTER THE SIZE :\n";
cin>>n;
cout<<"ENTER ARRAY ELEMENTS :\n";
for(i=1;i<=n;i++)
cin>>ar[i];

set(ar,n);
countsort();
int *x=get();
for(i=1;i<=n;i++)
{
ar[i]=*x;
x++;
}
cout<<"\nSORTED ARRAY IS :\n";
for(i=1;i<=n;i++)
cout<<ar[i]<<"\n";
cout<<"\nNO. OF COMPARISONS :"<<ct;
getch();
}

/*
OUTPUT:-
ENTER THE SIZE :
5
ENTER ARRAY ELEMENTS :
6
7
1
3
8

SORTED ARRAY IS :
1
3
6
7
8

NO. OF COMPARISONS :27


*/
/*
NAME-Himanshu Mundeepi
CLASS-B.Sc(h) Comp. Sci
ROLL NO.-7013

Q-WAP to sort a no.'s using heap sort.


*/

#include<iostream.h>
#include<conio.h>

int a[50],count;
void heap()
{
count=0;
}
int lefti(int i)
{
return (2*i);
}
int righti(int i)
{
return ((2*i)+1);
}
int heapsize;
void input(int);
void maxheapify(int);
void buildheap(int);
void heapsort(int);
void output(int);

void input(int n)
{
heapsize=n;
cout<<"\nENTER THE ELEMENTS OF ARRAY :\n";
for(int i=1;i<=n;i++)
cin>>a[i];
}
void maxheapify(int i)
{
int left=lefti(i);
int right=righti(i);
int large=i;
if(left<=heapsize&&a[left]>a[i])
{
large=left;
count++;
}
else
{
large=i;
count++;
}
if(right<=heapsize&&a[right]>a[large])
{
large=right;
count++;
}
if(large!=i)
{
int t=a[i];
a[i]=a[large];
a[large]=t;
maxheapify(large);
}
}
void buildheap(int n)
{
heapsize=n;
for(int j=heapsize/2;j>=1;j--)
maxheapify(j);
}
void heapsort(int n)
{
buildheap(n);
for(int i=n;i>=2;i--)
{
int t=a[1];
a[1]=a[i];
a[i]=t;
heapsize=heapsize-1;
maxheapify(1);
}
}
void output(int n)
{
cout<<"\nSORTED ARRAY IS :\n";
for(int i=1;i<=n;i++)
cout<<" "<<a[i]<<"\n";
cout<<"\nNO. OF COMPARISONS : "<<count;
}
void main()
{
clrscr();
int n;
cout<<"ENTER SIZE OF THE ARRAY : ";
cin>>n;
input(n);
heapsort(n);
output(n);
getch();
}

/*
OUTPUT:-
ENTER SIZE OF THE ARRAY : 5

ENTER THE ELEMENTS OF ARRAY :


6
4
1
8
7

SORTED ARRAY IS :
1
4
6
7
8

NO. OF COMPARISONS : 13

*/
/*
NAME-Himanshu Mundeepi
CLASS-B.Sc(h) Comp. Sci
ROLL NO.-7013

Q-WAP to sort a no.'s using insertion sort.


*/

#include<iostream.h>
#include<conio.h>

void main()
{ clrscr();
int a[50],n,tmp,j,i;
cout<<"ENTER SIZE OF ARRAY :\n";
cin>>n;
cout<<"ENTER ELEMENTS OF ARRAY :\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{ tmp=a[i];
j=i-1;
while((j>=0)&&(tmp<a[j]))
{ a[j+1]=a[j];
j--;
}
a[j+1]=tmp;

}
cout<<"SORTED ARRAY IS :\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}

/*
OUTPUT:-
ENTER SIZE OF ARRAY :
5
ENTER ELEMENTS OF ARRAY :
6
7
2
8
4
SORTED ARRAY IS :
2 4 6 7 8
*/
/*
NAME-Himanshu Mundeepi
CLASS-B.Sc(h) Comp. Sci
ROLL NO.-7013

Q-WAP to sort a no.'s using quick sort.


*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int arr[80],r;
void insert(int a);
void show(int a);
int partition(int p,int q);
void quicksort(int p,int q);

void insert(int a)
{
cout<<"\nenter the element of the array\n";
for(int i=1;i<=a;i++)
cin>>arr[i];
}
void show(int a)
{
cout<<"\nsorted array is\n";
for(int i=1;i<=a;i++)
cout<<" "<<arr[i]<<"\n";
}
int partition(int p,int q)
{
int temp;
int x=arr[q];
int i=p-1;
for(int j=p;j<=q-1;j++)
{
if(arr[j]<=x)
{
i=i+1;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
int temp1=arr[i+1];
arr[i+1]=arr[q];
arr[q]=temp1;
return i+1;
}
void quicksort(int p,int q)
{
if(p<q)
{
r=partition(p,q);
quicksort(p,r-1);
quicksort(r+1,q);
}
}
void main()
{
clrscr();
int a;
cout<<"enter the size of array";
cin>>a;
insert(a);
quicksort(1,a);
show(a);
getch();
}

/*
OUTPUT:-
ENTER THE SIZE OF ARRAY6

ENTER THE ELEMENTS OF THE ARRAY


4
8
7
2
5
3

SORTED ARRAY IS
2
3
4
5
7
8

*/
/*
NAME-Himanshu Mundeepi
CLASS-B.Sc(h) Comp. Sci
ROLL NO.-7013

Q-WAP for activity selection.


*/

//Program to schedule activities


#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int a[20];
int k;
int count;

int n;
int f[20];
int s[20];
int key[20];
void activity()
{ k=1;
}
void setdata();
void sort();
void setcount()
{
count=0;
}
void getcount();
void activity_selector();
void getdata();

//to input array on console


void setdata()
{
cout<<"\nEnter the no of activities ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"\nEnter the start time of activity "<<i<<" ";
cin>>s[i];
cout<<"\nEnter the finish time of activity "<<i<<" ";
cin>>f[i];
key[i]=i;
}
}

//to get number of comparisons


void getcount()
{
cout<<"\nThe number of comparisons is "<<count;
}
//function to sort arrays according to their finish times
void sort()
{
int temp;
for(int i=2;i<=n;i++)
{
for(int j=i-1;j>0;j--)
{
if(f[j]>f[j+1])
{
temp=f[j];
f[j]=f[j+1];
f[j+1]=temp;

temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;

temp=key[j];
key[j]=key[j+1];
key[j+1]=temp;
}
}
}
cout<<"\nActivities with their sorted finish times are :";
cout<<"\nActivity Start Finish";
for(i=1;i<=n;i++)
cout<<"\n"<<key[i]<<" "<<s[i]<<" "<<f[i];
}

//function to schedule activities


void activity_selector()
{
a[1]=1;
int i=1;
for(int m=2;m<=n;m++)
{ count++;
if(s[m]>=f[i])
{
a[++k]=m;
i=m;
}
}
}

//to output array on console


void getdata()
{
cout<<"\nSelected activities are";
for(int i=1;i<=k;i++)
cout<<" "<<a[i];
}
void main()
{
clrscr();
setdata();
setcount();
sort();
activity_selector();
getdata();
getcount();
getch();
}

/*
OUTPUT:

Enter the no of activities 2

Enter the start time of activity 1 8

Enter the finish time of activity 1 5

Enter the start time of activity 2 9

Enter the finish time of activity 2 15

Activities with their sorted finish times are :


Activity Start Finish
1 8 5
2 9 15
Selected activities are 2
The number of comparisons is 1
*/
/*
NAME-Himanshu Mundeepi
CLASS-B.Sc(h) Comp. Sci
ROLL NO.-7013

Program to find the optimal time and path through stations in an assembly
line*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class assembly
{
public:
int f[3][11];
int l[3][11];
int a[3][11];
int t[3][11];
int e[3];
int x[3];
int count;
assembly()
{
nl=2;

}
int o1,o2;
int n;
int nl;
int lf;
int ff;
void input();
void output();
void getcount();
void fastestway();
void fastest_way(int [3][11],int [3][11],int [],int [],int );
void printstations(int [3][11],int);
};

//to get number of comparisons


void assembly::getcount()
{
cout<<"\nThe number of comparisons is "<<count;
}
//to input array from console
void assembly::input()
{
cout<<" \nENTER THE NO OF STATIONS IN THE ASSEMBLY : ";
cin>>n;

cout<<"\nENTER THE TIME AT EACH STATION ";


for(int i=1;i<=nl;i++)
for(int j=1;j<=n;j++)
{
cout<<"\nLine "<<i<<" Station "<<j<<":";
cin>>a[i][j];
}
cout<<"\nENTER THE TRANSFER TIME :";
for(i=1;i<=nl;i++)
for(j=1;j<=n;j++)
{
cout<<"\nLine "<<i<<" Station "<<j<<":";
cin>>t[i][j];
}

for(int k=1;k<=nl;k++)
{
cout<<"\nEnter the time taken to go through the first
station ";
cout<<"\nOn LIne "<<k<<":";
cin>>e[k];
}

for(k=1;k<=nl;k++)
{
cout<<"\nEnter the time to leave assembly line from last
station";
cout<<"\nOn line"<<k<<":";
cin>>x[k];

}
}
void assembly::fastestway()
{
fastest_way(a,t,e,x,n);
}
//to compute optimal time through the stations
void assembly::fastest_way(int [3][11],int [3][11],int [],int [],int )
{

int z1,z2,z3,z4;

f[1][1]= e[1] + a[1][1];


f[2][1]= e[2] + a[2][1];

for(int j=2;j<=n;j++)
{
z1=f[1][j-1] + a[1][j];
z2=f[2][j-1]+t[2][j-1] +a[1][j];
z3=f[2][j-1] + a[2][j];
z4=f[1][j-1] + t[1][j-1] + a[2][j];
count++;
if (z1<=z2)
{
f[1][j]=z1;
l[1][j]=1;
}
else
{
f[1][j]=z2;
l[1][j]=2;
}
count++;
if(z3<=z4)
{
f[2][j]=z3;
l[2][j]=2;
}
else
{
f[2][j] = z4;
l[2][j] = 1;
}

}
o1 = f[1][n] +x[1];
o2 = f[2][n] +x[2];

if (o1<=o2)
{
ff=o1;
lf=1;
}
else
{
ff=o2;
lf=2;
}
cout<<"\n THE MINIMUM TIME TAKEN IS "<<ff;
cout<<endl;
}
//to compute optimal path through the stations
void assembly::printstations(int l[3][11],int no)
{
int i=lf;
cout<<"\n LINE "<<i<<" STATION "<<no;
for(int j=no;j>=2;j--)
{
i=l[i][j];
cout<<"\n LINE "<<i<<" STATION "<<j-1;
}

}
void main()
{
clrscr();
assembly a1;
a1.input();
a1.fastestway();
cout<<"\n\n THE PATH FOLLOWED WAS ";
a1.printstations(a1.l,a1.n);
a1.getcount();
getch();
}

/*
output:

ENTER THE NO OF STATIONS IN THE ASSEMBLY : 3

ENTER THE TIME AT EACH STATION


Line 1 Station 1:4

Line 1 Station 2:6

Line 1 Station 3:2

Line 2 Station 1:1

Line 2 Station 2:3

Line 2 Station 3:6

ENTER THE TRANSFER TIME :


Line 1 Station 1:4
Line 1 Station 2:6

Line 1 Station 3:3

Line 2 Station 1:4

Line 2 Station 2:7

Line 2 Station 3:3

Enter the time taken to go through the first station


On LIne 1:3

Enter the time taken to go through the first station


On LIne 2:7

Enter the time to leave assembly line from last station


On line1:2

Enter the time to leave assembly line from last station


On line2:5

THE MINIMUM TIME TAKEN IS 17

THE PATH FOLLOWED WAS


LINE 1 STATION 3
LINE 1 STATION 2
LINE 1 STATION 1
The number of comparisons is 12174
*/
/*
NAME-Himanshu Mundeepi
CLASS-B.Sc(h) Comp. Sci
ROLL NO.-7013

Q-Program to find best way of multiplying chain of matrices


*/

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class matrix
{
long m[11][11];
int s[11][11];
int p[10];
int count;
public:
int n;
void setdata();
void setcount()
{
count=0;
}
void getcount();
void matrix_chain();
void print_parens(int,int);
};

//function to input array from console


void matrix::setdata()
{
cout<<"ENTER THE LENGTH OF MATRIX CHAIN (MAX 10) :";
cin>>n;
cout<<"\nEnter the order of the matrices";
for(int i=0;i<=n;i++)
{
cout<<"\n enter p["<<i<<"]=";
cin>>p[i];
}
}
//function to get number of comparisons
void matrix::getcount()
{
cout<<"\nThe number of comparisons is "<<count;
}
//function to compute optimal scalar multiplications
void matrix::matrix_chain()
{
for(int i=1;i<=n;i++)
{ count++;
m[i][i]=0;
}
for(int l=2;l<=n;l++)
{
for(int i=1;i<=n-l+1;i++)
{
int j=i+l-1;
count++;
m[i][j]=300000;

for(int k=i;k<=j-1;k++)
{
count++;
long q=m[i][k]+m[k+1][j] + p[i-1]*p[k]*p[j];
if(q<m[i][j])
{
m[i][j]=q;
count++;
s[i][j]=k;
count++;
}

}
}
}
cout<<"\nTHE MINIMUM SCALAR MULTIPLICATIONS REQURIED ARE "<<m[1][n];
cout<<"\n";
}
//function to print the optimal parenthesization
void matrix::print_parens(int i,int j)
{
if (i==j)
cout<<"A"<<i<<" ";
else
{
cout<<"(";
print_parens(i,s[i][j]);
print_parens(s[i][j]+1,j);
cout<<")";
}
}
void main()
{
clrscr();
matrix mx;
mx.setdata();
mx.setcount();
mx.matrix_chain();
cout<<"\nTHE OPTIMAL PARENTHESIZATION IS ";
mx.print_parens(1,mx.n);
mx.getcount();
getch();
}

/*
OUTPUT:
ENTER THE LENGTH OF MATRIX CHAIN (MAX 10) :5

Enter the order of the matrices


enter p[0]=7
enter p[1]=6

enter p[2]=9

enter p[3]=4

enter p[4]=7

enter p[5]=6

THE MINIMUM SCALAR MULTIPLICATIONS REQURIED ARE 720

THE OPTIMAL PARENTHESIZATION IS ((A1 (A2 A3 ))(A4 A5 ))


The number of comparisons is 63
*/

Das könnte Ihnen auch gefallen