Madf Ex 1
Madf Ex 1
Madf Ex 1
AIM: To implement some standard algorithms following DIVIDE and CONQUER strategy
1] a. Merge Sort
b. Binary Search
2] a. Minmax algorithm
b. Quick Sort
3]a. Finding Kth smallest element
b. Stressan’s Matrix Multiplication
THEORY:
The Divide-and-Conquer strategy suggests splitting the inputs into k distinct subsets, 1< k <
n, yielding k subproblems. These subproblems must be solved, and then a method must be
found to combine sub-solutions into a solution of the whole. If the sub problems are still
relatively large, then the divide-and- conquer strategy can possibly be reapplied. D And C is
initially invoked as D And C(P), where P is the problem to be solved. Small(P) is a Boolean-
valued function that determines whether the input size is small enough that the answer can
be computed without splitting. The subproblems P₁,Pz.....Pk Combine is a function that
determines the solution to P using the solutions to the k subproblem.
3. Combine: Combine the sub-problems to get the final solution of the whole problem.
For divide-and-conquer based algorithms that produce sub-problems of the same type as
the original problem,it is very natural to first describe such algorithms using recursion.
Some standard algorithms that follow Divide and Conquer algorithm are:- Quick Sort, Merge
Sort, Find MinMax elements.
1 a]:
//sorting a character array in ascending order and using binary
search algo
#include<stdio.h>
char arr[20],temp[20],x;
int n;
//char
arr[11]={'M','O','C','I','W','E','R','Y','B','J','P'},temp[11],x;
//int n=11;
int main()
{
int i, high, low,index,r;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("\nEnter the elements:\n ");
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%c", &arr[i]);
}
low=0;
high=n-1;
MergeSort(low, high);
if(h>mid)
for(k=j; k<=high;k++)
temp[i++]=arr[k];
else
for(k=h; k<=mid;k++)
temp[i++]=arr[k];
for(k=low; k<=high;k++)
arr[k]=temp[k];
OUTPUT:
C:\Users\Komal Ambe\Downloads\211105028\merge_asc.exe
--------------------------------
1 b]:
//using merge sort to sort an array of nos. in descending
order
#include<stdio.h>
int main()
{
int i, high, low;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
low=0;
high=n-1;
MergeSort(low, high);
return 0;
}
if(h>mid)
for(k=j; k<=high;k++)
temp[i++]=arr[k];
else
for(k=h; k<=mid;k++)
temp[i++]=arr[k];
for(k=low; k<=high;k++)
arr[k]=temp[k];
}
OUTPUT:
C:\Users\Komal Ambe\Downloads\211105028\merge_dsc.exe
--------------------------------
2 a]:
//to find max and min ele in an array
#include<stdio.h>
int arr[20];
int max, min, max1=0, min1=0;
void MaxMin(int i,int j,int *max,int *min)
{
if (i == j)
{
*max = arr[i];
*min = arr[i];
}
else if (i==j-1)
{
if (arr[i] > arr[j])
{
*max = arr[i];
*min = arr[j];
}
else
{
*max = arr[j];
*min = arr[i];
}
}
else
{
int mid = (i+j)/2; //to find mid
element
MaxMin(i,mid,max,min);
MaxMin(mid+1,j,&max1,&min1);
if (max1 > *max)
*max = max1;
};
}
int main()
{
int i,n;
printf("Enter the number of elements: ");
scanf("%d",&n);
return 0;
}
OUTPUT:
C:\Users\Komal Ambe\Downloads\211105028\maxmin.exe
2 b]:
// sorting an array with quicksort
#include<stdio.h>
int main()
{
// int i;
/*printf("Enter the total no. of elements:\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}*/
printf("Unsorted array:\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
quicksort(0,n-1);
printf("\n\n\nThe elements in the sorted order are:\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}
}
}
i++;
}while(arr[i]<=v);
do{
j--;
}while(arr[j]>v);
if(i<j)
{
interchange(arr,i,j);
}
}while(i<j);
arr[m]=arr[j];
arr[j]=v;
count++;
printf("\nAfter pass %d:\n",count);
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return j;
}
int p = arr[i];
arr[i] = arr[j];
arr[j] = p;
}
OUTPUT:
C:\Users\Komal Ambe\Downloads\211105028\quicksort.exe
Unsorted array:
55 11 33 23 -17 89 -11 72 43
After pass 1:
-11 11 33 23 -17 43 55 72 89
After pass 2:
-17 -11 33 23 11 43 55 72 89
After pass 3:
-17 -11 11 23 33 43 55 72 89
After pass 4:
-17 -11 11 23 33 43 55 72 89
After pass 5:
-17 -11 11 23 33 43 55 72 89
--------------------------------
3a i]
// finding kth smallest integer
#include<stdio.h>
#define MAX 10
int i,n,count=0,arr[20];
void interchange(int arr[], int i, int j)
{
int p = arr[i];
arr[i] = arr[j];
arr[j] = p;
}
int Partition(int arr[],int m, int p)
{
int v=arr[m],i=m,j=p;
do{
do{
i++;
}while(arr[i]<=v);
do{
j--;
}while(arr[j]>v);
if(i<j)
{
interchange(arr,i,j);
}
}while(i<j);
arr[m]=arr[j];
arr[j]=v;
count++;
printf("\nAfter pass %d:\n",count);
printf("The array with pivot=%d and j=%d is:",v,j);
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return j;
}
void select(int arr[], int k, int n)
{
int low=1, up=n+1;
arr[n+1]=9999;
while(1)
{
int j=Partition(arr,low,up);
if(k==j)
{
select(arr,k-1,n);
OUTPUT 3aI] i:
C:\Users\Komal Ambe\Downloads\211105028\kint.exe
After pass 1:
The array with pivot=23 and j=2 is:17 8 23 87 65 45 68 79 89 72 92
After pass 2:
The array with pivot=87 and j=8 is:17 8 23 72 65 45 68 79 87 89 92
After pass 3:
The array with pivot=72 and j=6 is:17 8 23 68 65 45 72 79 87 89 92
After pass 4:
The array with pivot=68 and j=5 is:17 8 23 45 65 68 72 79 87 89 92
The value of smallest element at 6 position is :68
--------------------------------
Process exited after 1.618 seconds with return value 2
Press any key to continue . . .
--------------------------------
3a ii]
// finding kth smallest character
#include<stdio.h>
int i,n,count=0;
char arr[20];
void interchange(char arr[], int i, int j)
{
char p = arr[i];
arr[i] = arr[j];
arr[j] = p;
}
do{
j--;
}while(arr[j]>v);
if(i<j)
{
interchange(arr,i,j);
}
}while(i<j);
arr[m]=arr[j];
arr[j]=v;
count++;
printf("\nAfter pass %d:\n",count);
printf("The array with pivot=%c and j=%d is:",v,j);
for(i=0;i<n;i++)
{
printf("%c ",arr[i]);}
return j;
}
void select(char arr[], int k, int n)
{
int low=0, up=n;
arr[n+1]=9999;
while(1)
{
int j=Partition(arr,low,up);
if(k==j)
{
printf("\nThe value of smallest element at %d
position is :",k+1);
printf("%c ", arr[j]);
return ;
}
else if(k<j)
up=j;
else
low=j+1;
}
}
int main()
{
int k;
char y;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: \n");
//for(i=0;i<n;i++)
scanf("%s",arr);
select(arr,k-1,n);
OUTPUT 3aII] i:
C:\Users\Komal Ambe\Downloads\211105028\kchar.exe
After pass 1:
The array with pivot=H and j=2 is:D A H U L J O T W Q Z
After pass 2:
The array with pivot=U and j=8 is:D A H Q L J O T U W Z
After pass 3:
The array with pivot=Q and j=6 is:D A H O L J Q T U W Z
After pass 4:
The array with pivot=O and j=5 is:D A H J L O Q T U W Z
The value of smallest element at 6 position is :O
--------------------------------
Process exited after 33.38 seconds with return value 2
Press any key to continue . . .
OUTPUT 3aII]ii:
C:\Users\Komal Ambe\Downloads\211105028\kchar.exe
After pass 1:
The array with pivot=H and j=2 is:D A H U L J O T W Q Z
After pass 2:
The array with pivot=D and j=1 is:A D H U L J O T W Q Z
The value of smallest element at 2 position is :D
--------------------------------
Process exited after 12.75 seconds with return value 2
Press any key to continue . . .