Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Madf Ex 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

EXPERIMENT 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.

This technique can be divided into the following three parts:

1. Divide: This involves dividing the problem into smaller sub-problems.

2. Conquer: Solve sub-problems by calling recursively until solved.

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;

void MergeSort(int low, int high);


void Merge(int low, int mid, int high);
int BinarySearch(char arr[],int low,int high,char x);

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);

printf("\nThe elements in sorted order are: ");


for(i=0;i<n;i++)
{
printf("%c", temp[i]);
}

printf("\n\nEnter the number of elements to be searched: ");


scanf("%d", &r);
for(i=0;i<r;i++)
{
printf("\nEnter the element which needs to be searched: ");
fflush(stdin);
scanf("%s", &x);
index=BinarySearch(temp,low,high,x);
if(index==-1)
printf("Element %c not found\n",x);
else
printf("Element %c found at position %d\n",x,index+1);
}
return 0;
}

void MergeSort(int low, int high)


{
int mid;
if(low<high)
{
mid = (low+high)/2;
MergeSort(low, mid);
MergeSort(mid+1, high);
Merge(low, mid, high);
}
}

void Merge(int low, int mid, int high)


{
int h=low, i=low, j= mid+1,k;
while((h<=mid) && (j<=high))
{
if(arr[h]<=arr[j])
{
temp[i++]=arr[h++];
}
else
{
temp[i++]=arr[j++];
}
}

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];

int BinarySearch(char arr[],int low,int high,char x)


{
int mid;
if(low==high)
{
if(arr[low]==x)
return low;
else
return 0;
}
else
{
mid=(low+high)/2;
if (arr[mid]==x)
return mid;
else if (x>arr[mid])
return BinarySearch(arr,mid+1,high,x);
else
return BinarySearch(arr,low,mid-1,x);
}

OUTPUT:
C:\Users\Komal Ambe\Downloads\211105028\merge_asc.exe

Enter the number of elements: 11

Enter the elements:


m
o
c
i
w
e
r
y
b
j
p

The elements in sorted order are: b c e i j m o p r w y

Enter the number of elements to be searched: 2

Enter the element which needs to be searched: r


Element r found at position 9

Enter the element which needs to be searched: f


Element f not found

--------------------------------

Process exited after 23.62 seconds with return value 3221225725

Press any key to continue . . .

1 b]:
//using merge sort to sort an array of nos. in descending
order
#include<stdio.h>

//int arr[10]={53,-20,23,11,92,66,-11,85,26,34}, temp[20],


n=10;
int arr[20],temp[20],n;
void MergeSort(int low, int high);
void Merge(int low, int mid, int high);

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);

printf("\nThe elements in sorted order are (descending):


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

return 0;
}

void MergeSort(int low, int high)


{
int mid;
if(low<high)
{
mid = (low+high)/2;
MergeSort(low, mid);
MergeSort(mid+1, high);
Merge(low, mid, high);
}
}

void Merge(int low, int mid, int high)


{
int h=low, i=low, j= mid+1,k;
while((h<=mid) && (j<=high))
{
if(arr[h]>=arr[j])
{
temp[i++]=arr[h++];
}
else
{
temp[i++]=arr[j++];
}
}

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

Enter the number of elements: 10

Enter the elements: 53


-20
23
11
92
66
-11
85
26
34

The elements in sorted order are (descending): 92 85 66 53 34 26 23 11 -11 -20

--------------------------------

Process exited after 30.57 seconds with return value 0

Press any key to continue . . .

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;

if (min1 < *min)


*min = min1;

};
}
int main()
{
int i,n;
printf("Enter the number of elements: ");
scanf("%d",&n);

printf("Enter the elements\n");


for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
MaxMin(0,n-1,&max,&min);
printf("The maximum element in the array is %d\n",max);
printf("The minimum element in the array is %d\n",min);

return 0;
}

OUTPUT:
C:\Users\Komal Ambe\Downloads\211105028\maxmin.exe

Enter the number of elements: 9


Enter the elements
55
11
33
23
-17
72
-11
89
43
The maximum element in the array is 89
The minimum element in the array is -17

Process exited after 20.49 seconds with return value 0


Press any key to continue . . .

2 b]:
// sorting an array with quicksort
#include<stdio.h>

int arr[9]={55,11,33,23,-17,89,-11,72,43}, n=9,i,count=0;


//int arr[20],n;
void quicksort(int p,int q);
int Partition(int arr[],int m, int p);
void interchange(int arr[],int i,int j);

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;
}

void quicksort(int p,int q)


{
int j ,count=0;
if(p<q)
{
j=Partition(arr,p,q+1);
quicksort(p,j-1);
quicksort(j+1,q);

}
}

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);
for(i=0;i<n;i++)
printf("%d ",arr[i]);

return j;
}

void interchange(int arr[], int i, int 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

The elements in the sorted order are:


-17 -11 11 23 33 43 55 72 89

--------------------------------

Process exited after 17.78 seconds with return value 0

Press any key to continue . . .

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)
{

printf("\nThe value of smallest element at %d position


is :",k);
printf("%d", arr[j]);
return;
}
else if(k<j)
up=j;
else
low=j+1;
}
}
int main()
{
int k;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: \n");
for(i=0;i<=n-1;i++)
scanf("%d",&arr[i]);
printf("Enter value of k (index position): ");
scanf("%d", &k);

select(arr,k-1,n);

OUTPUT 3aI] i:
C:\Users\Komal Ambe\Downloads\211105028\kint.exe

The elements in the array are:


23 92 72 87 65 45 68 79 89 17 8

Enter value of k (index position): 6

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 . . .

OUTPUT 3aI] ii:


C:\Users\Komal Ambe\Downloads\211105028\kint.exe

The elements in the array are:


23 92 72 87 65 45 68 79 89 17 8

Enter value of k (index position): 10


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=89 and j=9 is:17 8 23 72 65 45 68 79 87 89 92
The value of smallest element at 10 position is :89

--------------------------------

Process exited after 2.016 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;
}

int Partition(char arr[],int m, int p)


{
char v=arr[m];
int 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=%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);

printf("Elements present in the array:\n");


for(i=0;i<n;i++)
printf("%c ",arr[i]);

printf("\nEnter value of k (index position): ");


scanf("%d", &k);

select(arr,k-1,n);

OUTPUT 3aII] i:
C:\Users\Komal Ambe\Downloads\211105028\kchar.exe

Enter the number of elements: 11


Enter the elements:
HZQULJOTWDA
Elements present in the array:
HZQULJOTWDA

Enter value of k (index position): 6

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

Enter the number of elements: 11


Enter the elements:
HZQULJOTWDA

Elements present in the array:


HZQULJOTWDA

Enter value of k (index position): 2

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 . . .

You might also like