Professional Documents
Culture Documents
Daa
Daa
Management
studies
Meerut (India)
Practical of
Data Design and Algorithm
RCA 352
Submitted to Submitted By
Pawan Kumar Goyal sir Shubham
Sharma
Astt Professor 1900740149065
CONTENTS
S.NO. NAME OF THE PROGRAM DATE REMARKS
#include (stdio.h)
#include (conio.h)
#define MAXSIZE 500
voidquickSort (int elements [], int maxsize);
void sort(int elements[], int left, int right);
int elements[MAXSIZE];
int main()
{
int i, maxsize;
printf(“\nHow many elements you want to sort: “);
scanf(“%d”,&maxsize);
printf(“\nEnter the values one by one: “);
for (i = 0; i < maxsize; i++)
{
printf (“\nEnter element %i :”,i);
scanf(“%d”,&elements[i]);
}
printf(“\nArray before sorting:\n”);
for (i = 0; i < maxsize; i++)
printf(“[%i], “,elements[i]);
printf (“\n”);
quickSort(elements, maxsize);
printf(“\nArray after sorting:\n”);
for (i = 0; i < maxsize; i++)
printf(“[%i], “, elements[i]);
}
void quickSort(int elements[], int maxsize)
{
sort (elements, 0, maxsize - 1);
}
void sort (int elements[], int left, int right)
{
int pivot, l, r;
l = left;r = right;
pivot = elements[left];
while (left < right)
{
while ((elements[right] >= pivot) && (left < right))
if (left != right)
{
elements[left] = elements[right];
left++;
}
while ((elements[left] <= pivot) && (left < right))
left++;
if (left != right)
{
elements[right] = elements[left];
}
}
elements[left] = pivot;
pivot = left;left = l;right = r;
if (left < pivot)
sort(elements, left, pivot - 1);
int M = atoi(argv[1]);
int N = atoi(argv[2]);
int P = atoi(argv[3]);
int i, j;
PROGRAM: - 4
WRITE APROGRAM TO IMPLEMENT THE 0-1 KNAPSACK PROBLEM.
#include <stdio.h>
#define MAXWEIGHT 100
intn=3;
intc[10]={8,6,4};
intv[10]={16,10,7};
intW=10;
voidfill_sack()
{
inta[MAXWEIGHT];
intlast_added[MAXWEIGHT];
inti,j;
intaux;
for(i=0;i<=W;++i)
{
a[i]=0;
last_added[i]=-1;
}
a[0]=0;
for(i=1;i<=W;++i)
for(j=0;j<n;++j)
if((c[j]<=i)&&(a[i]<a[i-c[j]]+v[j]))
{
a[i]=a[i-c[j]]+v[j];
last_added[i]=j;
}
for(i=0;i<=W;++i)
if(last_added[i]!=-1)
printf("Weight %d; Benefit: %d; To reach this weight I added object %d (%d$ %dKg) to
weight %d.\n",i,a[i],last_added[i]+1,v[last_added[i]],c[last_added[i]],i-c[last_added[i]]);
else
printf("Weight %d; Benefit: 0; Can't reach this exact weight.\n",i);
printf("---\n");
aux=W;
while((aux>0)&&(last_added[aux]!=-1)){
printf("Added object %d (%d$ %dKg). Space left: %d\n",last_added[aux]
+1,v[last_added[aux]],c[last_added[aux]],aux-c[last_added[aux]]);
aux-=c[last_added[aux]];
}
#include<iostream>
#include<list>
using namespace std;
class node
{
public:
int d,p,t;
bool operator < (node n)
{
if(p<n.p)
return 1;
else
return 0;
}
};
int main()
{int d,p,t,i,profit,min,n;
list<node> lst;
node pt;
cout<<"Enter no of entries";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter data(p,d,t)";
cin>>p>>d>>t;
pt.d=d;pt.p=p;pt.t=t;
lst.push_back(pt);
}
int maxd=0;
lst.sort();
lst.reverse();
cout<<"sorted list";
list<node> :: iterator itr=lst.begin();
while(itr!=lst.end())
{
pt=*itr;
if(maxd<pt.d) maxd=pt.d;
cout<<pt.p<<endl<<pt.d<<endl<<pt.t<<endl<<endl;
itr++;
}
//cout<<" max deadline "<<maxd<<endl;
itr=lst.begin();
profit=0;
while(itr!=lst.end())
{
pt=*itr;
if(pt.d>=pt.t)
min=pt.t;
elsemin=pt.d;
if(maxd>=min)
profit+=min*pt.p;
else
profit+=maxd*pt.p;
itr++;maxd=maxd-min;
}
cout<<"profit is : "<<profit<<endl;
return 0;
}
PROGRAM: - 7
WRITE APROGRAM TO IMPLEMENT THE OPTIMAL MERGE PATTERNS.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,k,a[10],c[10],n,l;
cout<<"Enter the no. of elements\t";
cin>>n;
cout<<"\nEnter the sorted elments for optimal merge pattern";
for(i=0;i<n;i++)
{
cout<<"\t";
cin>>a[i];
}
i=0;k=0;
c[k]=a[i]+a[i+1];
i=2;
while(i<n)
{
k++;
if((c[k-1]+a[i])<=(a[i]+a[i+1]))
{
c[k]=c[k-1]+a[i];
}
else
{
c[k]=a[i]+a[i+1];
i=i+2;
while(i<n)
{ k++;
if((c[k-1]+a[i])<=(c[k-2]+a[i]))
{
c[k]=c[k-1]+a[i];
}
else
{
c[k]=c[k-2]+a[i];
}i++;
}
}i++;
}
k++;
c[k]=c[k-1]+c[k-2];
cout<<"\n\nThe optimal sum are as follows......\n\n";
for(k=0;k<n-1;k++)
{
cout<<c[k]<<"\t";
}
l=0;
for(k=0;k<n-1;k++)
{
l=l+c[k];
}
cout<<"\n\n The external path length is ......"<<l;
getch();
}
PROGRAM: - 8
WRITE APROGRAM FOR CREATING MINIMUM SPANNING TREE FROM
PRIM'SALGORITHM.
#include<stdio.h>
#define MAX 10
#define TEMP 0
#define PERM 1
#define FALSE 0
#define TRUE 1
#define infinity 9999
struct node
{
int predecessor;
int dist;
int status;
};
struct edge
{
int u;
int v;
};
int adj[MAX][MAX];
int n;
main()
{
int i,j;
int path[MAX];
int wt_tree,count;
struct edge tree[MAX];
create_graph();
printf("Adjacency matrix is :\n");
display();
count = maketree(tree,&wt_tree);
printf("Weight of spanning tree is : %d\n", wt_tree);
printf("Edges to be included in spanning tree are : \n");
for(i=1;i<=count;i++)
{
printf("%d->",tree[i].u);
printf("%d\n",tree[i].v);
}
}
create_graph()
{
int i,max_edges,origin,destin,wt;
printf("Enter number of vertices : ");
scanf("%d",&n);
max_edges=n*(n-1)/2;
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d(0 0 to quit) : ",i);
scanf("%d %d",&origin,&destin);
if((origin==0) && (destin==0))
break;
printf("Enter weight for this edge : ");
scanf("%d",&wt);
if( origin > n || destin > n || origin<=0 || destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
{
adj[origin][destin]=wt;
adj[destin][origin]=wt;
}
}
if(i
{
printf("Spanning tree is not possible\n");
exit(1);
}
}
display()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d",adj[i][j]);
printf("\n");
}
}
current=1;
count=0;
while( all_perm(state) != TRUE )
{
for(i=1;i<=n;i++)
{
if ( adj[current][i] > 0 && state[i].status == TEMP )
{
if( adj[current][i] < state[i].dist )
{
state[i].predecessor = current;
state[i].dist = adj[current][i];
}
}
}
min=infinity;
for(i=1;i<=n;i++)
{
if(state[i].status == TEMP && state[i].dist < min)
{
min = state[i].dist;
current=i;
}
}
state[current].status=PERM;
u1=state[current].predecessor;
v1=current;
count++;
tree[count].u=u1;
tree[count].v=v1;
*weight=*weight+adj[u1][v1];
}
return (count);
}
int all_perm(struct node state[MAX] )
{
int i;
for(i=1;i<=n;i++)
if( state[i].status == TEMP )
return FALSE;
return TRUE;
}
PROGRAM: - 9
Write a Program for creating a minimum spanning tree from
Kruskal'salgorithm.
#include
#define MAX 20
struct edge
{
int u;
int v;
int weight;
struct edge *link;
}
*front = NULL;
int father[MAX];
struct edge tree[MAX];
int n;
int wt_tree=0;
int count=0;
void make_tree();
void insert_tree(int i,int j,int wt);
void insert_pque(int i,int j,int wt);
struct edge *del_pque();
main()
{
int i;
create_graph();
make_tree();
printf("Edges to be included in spanning tree are :\n");
for(i=1;i<=count;i++)
{
printf("%d->",tree[i].u);
printf("%d\n",tree[i].v);
}
printf("Weight of this minimum spanning tree is : %d\n", wt_tree);
}
create_graph()
{
int i,wt,max_edges,origin,destin;
void make_tree()
{
struct edge *tmp;
int node1,node2,root_n1,root_n2;
while( count < n-1)
{
tmp=del_pque();
node1=tmp->u;
node2=tmp->v;
printf("n1=%d ",node1);
printf("n2=%d ",node2);
if(root_n1!=root_n2)
{
insert_tree(tmp->u,tmp->v,tmp->weight);
wt_tree=wt_tree+tmp->weight;
father[root_n2]=root_n1;
}
}
}
void insert_tree(int i,int j,int wt)
{
printf("This edge inserted in the spanning tree\n");
count++;
tree[count].u=i;
tree[count].v=j;
tree[count].weight=wt;
}
void insert_pque(int i,int j,int wt)
{
struct edge *tmp,*q;
PROGRAM: - 10
WRITE APROGRAM TO IMPLEMENT THE N-QUEEN'S PROBLEM.
#include <iostream.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void check(int, int, char [100][100]);
void print(char [100][100]);
int no_of_queens, queen = 2, flagrow = 0, flagcol = 0;
int count = 1;
char ch, response_row, response_col;
int main(void)
{
int row, col, i;
char board[100][100], response;
clrscr ();
Printf ("@@ This is n-queen problem.Enter the number ofqueens (say n) and watch how
computer places them in (n x n) matrixsuch that none can meet another moving along
horizontally, verticallyor digonally.");
printf("Enter the number of queens : ");
scanf("%d", &no_of_queens);
if(no_of_queens > 23)
{
Printf (" @@ Thought the program is OK for any queen value. Butdue the configuration
of the output screen the output will betranketed (A very large queen number may cause
the system stackoverflow).So it is highly recommended that you run the program
withmaximum queen number 23...");
printf("Want to continue(Y/N)?");
fflush(stdin);
scanf("%c", &response);
if(toupper(response) == 'N')
return (0);
}
else if(no_of_queens < 3)
{
printf("The number of Queen must be greater than 3.");
getch();
return (0);
}
printf("Want a row number below the board(Y/N) : ");
fflush(stdin);
response_row = (char)getchar();
if(toupper(response_row) == 'Y')
flagrow = 1;
printf("Want a column number below the board(Y/N) : ");
fflush(stdin);
response_col = (char)getchar();
if(toupper(response_col) == 'Y')
flagcol = 1;
clrscr();
printf("M/c in work ! Please Wait...");
// This for-loop is used for checking all the columns of row 0 only...
setcursortype(_NOCURSOR);
for(col = 0; col < no_of_queens; col++)
{
memset(board, '-', sizeof(board));
check( 0, col, board );
}
clrscr();
printf("Thank you for seeing this program through.");
getch();
return (0);
}
// Vertical check...
for(i = 0; i < r; i++)
{
if ( board[i][c] == queen)
return;
}
// Horizontal check...
for(j = 0; j < c; j++)
{
if ( board[r][j] == queen)
return;
}
// Left-Diagonal check...
i = r; j = c;
do
{
if ( board[i][j] == queen )
return;
i--; j--;
}
while( i >= 0 && j >= 0 );
// Right-Diagonal check...
i = r; j = c;
do
{
if ( board[i][j] == queen )
return;
i--; j++;
}
while( i >= 0 && j < no_of_queens );
// This for-loop is used for checking all the columns for each row
//starting from 1 upto the end...
for(int p = 0; p < no_of_queens; p++)
check(r, p, board);
for(int h = 0; h < no_of_queens; h++)
board[r - 1][h] = '-';
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
void createProfit(int profitMatrix[20][20]);
void createRoute(int currentRoute[20]);
int evaluateRoute(int currentRoute[20], const int profitMatrix[20][20]);
void tryRoute(int currentRoute[20], int bestRoute[20], const int profitMatrix[20][20]);
void swap(int &item1, int &item2);
int main()
{
// variables used
int profitMatrix[20][20];
int currentRoute[20];
int bestRoute[20];
int value=0;
int max=0;
int i=0;
long int start;
int kount=0;
return 0;
}
// tryRoute - tries a route. You get to pick what route to try next.
//
// inputs - currentRoute - the current route plan
// - bestRoute - the best route so far
// - profitMatrix - the matrix used to calculate the profit for a route
//
// outputs - currentRoute - update this plan for you current route
// - bestRoute - Update this plan for the best route you have seen.
// - profitMatrix - Changes to profitMatrix ARE NOT ALLOWED
void tryRoute(int currentRoute[20], int bestRoute[20], const int profitMatrix[20][20])
{
// variables
int planRoute[20];
int i;
int first;
int second;
static long int tries=0; // inializes to zero the first time only. (static)
// 90% of the time start over, otherwise see if we can plan a better route
// based on the current route.
if (rand() < 32767*.90)
{
// random route
createRoute(planRoute);
}
else
{
numb=rand()%10+5;
for (i=1; i<=numb; i++)
{
first=rand()%19+1;
second=rand()%19+1;
swap(route[first],route[second]);
}
}
PROGRAM: - 12
WRITE APROGRAM TO IMPLEMENT THE INSERTION SORT.
#include<stdio.h>
void main()
{
int A[20], N, Temp, i, j;
clrscr();
printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
scanf("%d", &N);
printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
for(i=0; i<N; i++)
{
gotoxy(25,11+i);
scanf("\n\t\t%d", &A[i]);
}
for(i=1; i<N; i++)
{
Temp = A[i];
j = i-1;
while(Temp<A[j] && j>=0)
{
A[j+1] = A[j];
j = j-1;
}
A[j+1] = Temp;
}
printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
for(i=0; i<N; i++)
printf("\n\t\t\t%d", A[i]);
getch();
PROGRAM: - 13
#include<stdio.h>
void restoreHup(int*,int);
void restoreHdown(int*,int,int);
void main()
{
int a[20],n,i,j,k;
printf("
Enter the number of elements to sort : ");
scanf("%d",&n);
printf("
Enter the elements :
");
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
restoreHup(a,i);
}
j=n;
for(i=1;i<=j;i++)
{
int temp;
temp=a[1];
a[1]=a[n];
a[n]=temp;
n--;
restoreHdown(a,1,n);
}
n=j;
printf("
Here is it...
");
for(i=1;i<=n;i++)
printf("%4d",a[i]);
}
while((i>1)&&(a[i/2]<v))
{
a[i]=a[i/2];
i=i/2;
}
a[i]=v;
}
a[j/2]=a[j];
j=j*2;
}
a[j/2]=v;
}
PROGRAM: - 14
WRITE APROGRAM TO IMPLEMENT THE BUBBLE SORT METHOD.
#include<stdio.h>
#include<conio.h>
main()
{ int arr[50],temp,i,j,n;
clrscr();
printf("\nEnter any Value less Than 50");
scanf("%d",&n);
printf("\n\tEnter The Values into ARRAY ");
for(i=0;i<n;i++)
{ printf("\n\n Enter Element no %d: ",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<n-1;i++)
{for(j=0;j<n-i-1;j++)
{ if(arr[j] >arr[j+1])
{ temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}}}
printf("\n\n-- Sorted Series --");
for(i=0;i<n;i++)
{
printf("\n \n \t %d",arr[i]);
}
getch();
}
PROGRAM: - 15
WRITE APROGRAM TO IMPLEMENT THE SEQUENTIAL SEARCH.
#include <stdio.h>
#define MAX 24
void createIndex(int index[],int isize,int arr[],int asize)
{
int i,j;
for(i=0,j=0;i<asize;i+=8,j++)
{
ndex[j]= arr[i];
}
index[j] = arr[asize-1]; }
int indexSeqSearch(int val, int index[], int isize, int arr[], int asize)
{
int i=0,j=0,pos=0;
int high=0,low=0;
if(val > index[isize-1] && val < index[0])
return -1;
while(i<isize)
{
if(val == index[i])
{
pos = 8 * i;
return pos;
}
if(val < index[i])
{
low = 8 * (i-1);
high = 8 * i;
break;
}
else
{
low = 8 * i;
high = 8 * (i+1);
}
i++;
}
while(low < high)
{
if(val == arr[low])
return low;
else
low++;
}
return -1;
}
int main()
{
int arr[MAX]={ 8,14,26,38,72,115,306,
321,329,387,409,512,540,567,583,592,602,611,618,741,798,811,814,876};
int index[(MAX/8)+1]={0};
createIndex(&index[0],(MAX/8)+1,&arr[0],MAX);
int opt=0,pos=0;
while(opt < MAX)
{
pos = indexSeqSearch(arr[opt],&index[0],(MAX/8)+1,&arr[0],MAX);
if( pos != -1)
{
printf("\n%d found at position %d",arr[opt],pos);
}
else
printf("\n%d not found.",arr[opt]);
opt++;
}
return 0;
}
PROGRAM: - 16
WRITE APROGRAM TO IMPLEMENT THE BINARY SEARCH [INT ARRAY].
#include <stdio.h>
#define TRUE 0
#define FALSE 1
PROGRAM: - 17
WAP TO FIND THE kth SMALLEST ELEMENT IN THE GIVEN LIST OF ARRAY
ELEMENTS.
#include <stdio.h>
#include<conio.h>
main ()
{
int a[20], i,n;
printf ("ent how many elements u want to enter");
scanf ("%d",&n);
printf ("enter the elements");
for (i=1;i<=n;i++)
scanf ("%d",&a[i]));
s=a [1];
for (i=2;i<=n;i++)
{
if
{
s>a[i]
s=a[i]
}
}
Printf ("the smallest element is %d",s);
}