Sie sind auf Seite 1von 17

Divide and Conquer

Merge Sort
Quick Sort
Maximum and Minimum

MERGE SORT
#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
void main()
{
int a[30],n,i;
clrscr();
printf("enter the no.of elements:");
scanf("%d",&n);
printf("enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\n sorted array is:");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}

void merge(int a[],int i1,int j1,int i2,int j2)


{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1&&j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[j++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

Output:
Enter the no. Of elements: 5
Enter array elements:
100
5
90
20
30

Sorted array is:


5
20
30
90
100

Quick Sort
#include<stdio.h>
#include<conio.h>
void quicksort(int array[20],int first,int last);
void main()
{
int array[30];
int i,size;
clrscr();
printf("enter the total number of elements:");
scanf("%d",&size);
printf("enter the total %d elements:\n",size);
for(i=0;i<size;i++)
scanf("%d",&array[i]);
quicksort(array,0,size-1);
printf("quicksorted elements are as:\n");
for(i=0;i<size;i++)
printf("%d\t",array[i]);
getch();
}
void quicksort(int array[20],int first,int last)
{
int i,j,pivot,temp;
if(first<last)
{

pivot=first;
i=first;
j=last;
while(i<j)
{
while(array[i]<=array[pivot]&&i<last)
i++;
while(array[j]>array[pivot])
j--;
if(i<j)
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
temp=array[pivot];
array[pivot]=array[j];
array[j]=temp;
quicksort(array,first,j-1);
quicksort(array,j+1,last);
}
}

MAXIMUM AND MINIMUM


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,max,min;
clrscr();
printf("enter the value of n:");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("enter the %delements",i+1);
scanf("%d",&a[i]);
}
max=a[0];
min=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
printf("\n the maximum number is %d\n",max);
printf("\n the minimum number is %d\n",min);
getch();
}

Greedy Method
Knap sack Problem
Tree Vertex Splitting
Job Sequencing

KNAPSACK PROBLEM
#include<stdio.h>
#include<conio.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 profit of each object:");
for(i=0;i<num;i++)
{
scanf("%f%f",&weight[i],&profit[i]);
}
printf("\nenter the capacity of knapsack:");
scanf("%f",&capacity);
for(i=0;i<num;i++)
{
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]=temp;
temp=profit[j];
profit[j]=profit[i];
profit[i]=temp;
}
}
}
knapsack(num,weight,profit,capacity);
return(0);
}

TREE VERTEX SPLITTING


#include <stdio.h>
#include <stdlib.h>
int min(int x, int y)
{
return ( (x < y)? x: y);)
}
struct node
{
int data;
int vc;
struct node *left, *right;
};
int vCover(struct node *root)
{
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 0;
if (root->vc != 0)
return root->vc;
int size_incl = 1 + vCover(root->left) + vCover(root->right);
int size_excl = 0;
if (root->left)
size_excl += 1 + vCover(root->left->left) + vCover(root->left->right);
if (root->right)
size_excl += 1 + vCover(root->right->left) + vCover(root->right->right);
root->vc = min(size_incl, size_excl);
return root->vc;
}
struct node* newNode( int data )
{
struct node* temp = (struct node *) malloc( sizeof(struct node) );
temp->data = data;

temp->left = temp->right = NULL;


temp->vc = 0;
return temp;
}
int main()
{
struct node *root
root->left
root->left->left
root->left->right
root->left->right->left
root->left->right->right
root->right
root->right->right

= newNode(20);
= newNode(8);
= newNode(4);
= newNode(12);
= newNode(10);
= newNode(14);
= newNode(22);
= newNode(25);

printf ("Size of the smallest vertex cover is %d ", vCover(root));


return 0;
}