Sie sind auf Seite 1von 12

CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

1. Implementation of binary search using Divide-and-Conquer technique.


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
printf("Enter total number of elements :");
scanf("%d",&n);
printf("Enter %d number :", n);
for (i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter a number to find :");
scanf("%d", &search);
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] == search)
{
printf("%d found at location %d\n", search, middle+1);
break;
}
else if(arr[middle] < search)
{
first = middle + 1;

}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}

if(first > last)


{
printf("Not found! %d is not present in the list.",search);
}

getch();
}

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

2. Implementation of merge sort algorithms using Divide-and-Conquer technique.

#include<stdlib.h>
#include<stdio.h>

void merge(int arr[], int l, int m, int r)


{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[10], R[10];

for (i = 0; i < n1; i++)


L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];

i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if there


are any */

while (i < n1)


{
arr[k] = L[i];
i++;
k++;
}

/* Copy the remaining elements of R[], if there


are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

}
}

void mergeSort(int arr[], int l, int r)


{
if (l < r)
{
int m = l+(r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}

void printArray(int A[], int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

int main()
{
int arr[10],n,i;
clrscr();
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter the elements");
for (i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Given array is \n");
printArray(arr, n);
mergeSort(arr, 0, n - 1);
printf("\nSorted array is \n");
printArray(arr, n);
getch();
return 0;
}

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

3. Implementation of quick sort algorithms using Divide-and-Conquer technique.

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

void swap(int* a, int* b)


{
int t = *a;
*a = *b;
*b = t;
}

int partition (int arr[], int low, int high)


{
int pivot = arr[high];
int i = (low - 1);
int j;

for (j = low; j <= high- 1; j++)


{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

void quickSort(int arr[], int low, int high)


{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

void printArray(int arr[], int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}

int main()
{
int arr[10],n,i;

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

clrscr();
printf("Enter the number of elements");
scanf("%d",&n);

printf("Enter the elements");


for (i=0;i<n;i++)
scanf("%d",&arr[i]);

quickSort(arr, 0, n-1);
printf("Sorted array: n");
printArray(arr, n);
getch();
return 0;
}

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

4. Implementation of Knapsack using Greedy technique.

# include<stdio.h>

void knapsack(int n, float weight[], float profit[], float capacity) {


float x[20], tp = 0;
int i, j, u;
u = capacity;

for (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 + profit[i];
u = u - weight[i];
}
}

if (i < n)
x[i] = u / weight[i];

tp = tp + (x[i] * profit[i]);

printf("\nThe result vector is:- ");


for (i = 0; i < n; i++)
printf("%f\t", x[i]);

printf("\nMaximum profit is:- %f", tp);

int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
clrscr();
printf("\nEnter the no. of objects:- ");
scanf("%d", &num);

printf("\nEnter the wts and profits of each object:- ");


for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}

printf("\nEnter the capacityacity of knapsack:- ");


scanf("%f", &capacity);

for (i = 0; i < num; i++) {

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

ratio[i] = profit[i] / weight[i];


}

for (i = 0; i < num; i++) {


for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;

temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}

knapsack(num, weight, profit, capacity);


getch();
return(0);
}

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

5. Implementation of Single-Source Shortest Paths algorithms using Greedy technique.

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

int shortest(int ,int);


int cost[10][10],dist[20],i,j,n,k,m,S[20],v,totcost,path[20],p;
void main()
{
int c;
clrscr();
printf("enter no of vertices");
scanf("%d", &n);
printf("\nenter no of edges");
scanf("%d", &m);
printf("\nenter\nEDGE Cost\n ");

for(k=1;k<=m;k++)
{
scanf("%d%d%d", &i,&j,&c);
cost[i][j]=c;
}

for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]==0)
cost[i][j]=31999;

printf("\nenter initial vertex");


scanf("%d", &v);
printf("\n",v);
shortest(v,n);
getch();
}

int shortest(int v,int n)


{
int min;
for(i=1;i<=n;i++)
{
S[i]=0;
dist[i]=cost[v][i];
}
path[++p]=v;
S[v]=1;
dist[v]=0;
for(i=2;i<=n-1;i++)
{
k=-1;
min=31999;
for(j=1;j<=n;j++)
{

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

if(dist[j]<min && S[j]!=1)


{
min=dist[j];
k=j;
}
}
if(cost[v][k]<=dist[k])
p=1;
path[++p]=k;
for(j=1;j<=p;j++)
printf("%d",path[j]);
printf("\n");
S[k]=1;
for(j=1;j<=n;j++)
if(cost[k][j]!=31999 && dist[j]>=dist[k]+cost[k][j] && S[j]!=1)
dist[j]=dist[k]+cost[k][j];
}
return 0;
}

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

6. Implementation of Multi-Stage Graphs using Dynamic Programming technique.

#include<iostream.h>
#include<conio.h>
Int cost [20][20];
int main ()
{
Int I,j,k, l=1,x=1,n1,n2[10], ans[10][10], path[10], path2[20];
Cout<<”Enter no. of stages : ”;
Cin>>n1;
n2[0]=1;
n2[n1-1]=1;
n2[n1]=0;
for(i=1;i<n-1;i++)
{
Cout<<”Options for stage”<<i+1<<”:”;
Cin>>n2[i];
}
for(i=1;i<n1;i++)
{
X=x+n2[i-1];
for(j=o;j<n2[i-1;j++])
{
for(k=0;k<n2[i];k++)
{
Cout<<”path”<<L<<”-->”<<x+k<<”:”;
Cin>>cost[i][j][k];
}
l++
}
}
X=l+1;
Path[m1]=l;
ans[n1][0];
for (i=n-1;i<0;i++)
{
x=x-n2[i];
for(j=0;j<n2[i-1];j++)
{
ans[i][j]=32767;
for(k=0;k<na[i];k++)
{
If(cost[i][j][k]+ans[i+1][k]<ans[i][j]&&cost[i][j][k]=32767)
{
path2[l-1]=x+k;
ans[i][j]=cost[i][j][k]+ans[i+1][k];
}
}
Cout<<path2[l-1]<<”,”<<x;
l--;
}
}

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

Cout<<” Final answer is : ”<<ans[1][0];


Path[1]=1;
Cout<<” Path is : ”<<1<<”-->”;
For(i=2;i<n1;i++)
{
Path[i]=path[2][path[i-1]];
Cout<<path[i]<<”-->”;
}
Cout<<path[n1];
getch ();
return 0;
}

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry
CS P42- DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

7. Implementation of 0/1 Knapsack using Dynamic Programming technique.

#include<stdio.h>

int max(int a, int b)


{
return (a > b)? a : b;
}

int knapSack(int W, int wt[], int val[], int n)


{
int i, w;
int K[n+1][W+1];

for (i = 0; i <= n; i++)


{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}

return K[n][W];
}

void main()
{
int i, n, val[20], wt[20], W;
clrscr();
printf("Enter number of items:");
scanf("%d", &n);

printf("Enter value and weight of items:\n");


for(i = 0;i < n; ++i){
scanf("%d%d", &val[i], &wt[i]);
}

printf("Enter size of knapsack:");


scanf("%d", &W);

printf("%d", knapSack(W, wt, val, n));


getch();
}

P.Karthikeyan, M. Tech., (Ph. D),


AP/CSE, ACET, Pondicherry

Das könnte Ihnen auch gefallen