Sei sulla pagina 1di 4

LAB EXERCISE -4

Prog1: WAP to implement Counting Sort.


COUNTING_SORT (A, B, k)
for i 1 to k do
c[i] 0
for j 1 to n do
c[A[j]] c[A[j]] + 1
//c[i] now contains the number of elements equal to i
for i 1 to k do
c[i] c[i] + c[i-1]
// c[i] now contains the number of elements i
for j n downto 1 do
B[c[A[ j ]]] A[j]
c[A[ j]] c[A[j]] - 1
#include<stdio.h>
#include<conio.h>
void main()
{ int a[20],b[20],c[20],n,i,j;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("\nEnter the elements of the array in the range 1-9 : ");
for(i=1;i<=n;i++)
{ scanf("%d",&a[i]);}
for(i=1;i<=9;i++)
{c[i]=0;}
for(j=1;j<=n;j++)
{ c[a[j]]=c[a[j]]+1;}
for(i=1;i<=9;i++)
{ c[i]=c[i]+c[i-1];}
for(j=n;j>=1;j--)
{ b[c[a[j]]]=a[j];
c[a[j]]=c[a[j]]-1;}
printf("After Counting Sort:");
for(i=1;i<=n;i++)
{ printf("\t%d",b[i]);}
getch();}

Prog 2:- WAP to multiply two matrices using Strasen Matrix Multiplication
#include<stdio.h>
#include<conio.h>
void main()
{ int a[2][2],b[2][2],c[2][2],i,j,n,x1,x2,x3,x4,x5,x6,x7;
printf("Enter the elements of first array: ");
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
{ scanf("%d",&a[i][j]);
}}
printf("Enter the elements of second array: ");
for(i=0;i<2;i++)
{for(j=0;j<2;j++)
{ scanf("%d",&b[i][j]);
}}
x1=a[0][0]*(b[0][1]-b[1][1]);
x2=(a[0][0]+a[0][1])*b[1][1];
x3=(a[1][0]+a[1][1])*b[0][0];
x4=a[1][1]*(b[1][0]-b[0][0]);
x5=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
x6=(a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
x7=(a[0][0]-a[1][0])*(b[0][0]+b[0][1]);
c[0][0]=x5+x4-x2+x6;
c[0][1]=x1+x2;
c[1][0]=x3+x4;
c[1][1]=x1+x5-x3-x7;
printf("The result using Strassen Matrix Multiplication is:-\n");
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
{ printf("%d ",c[i][j]);}
printf("\n");
} getch();}
Output:-

Prog 3:- WAP to find the minimum and maximum element from an array using Divide and
Conquer Approach.

dcminmax( i, j,min,max)
{ if(i==j)
maxmina[i];
else
{ if(i == j-1)
{ if(a[i] <a[j])
{
max a[j];
min a[i];
}
else
{
max a[i];
min a[j];
}
}
else
{
mid (i+j)/2;
maxmin(i, mid);
max1 max; min1 min;
maxmin(mid+1, j);
if(max <max1)
max max1;
if(min > min1)
min min1;
}
}
}
#include<stdio.h>
int max, min;
int a[100];
void maxmin(int i, int j)
{
int max1, min1, mid;
if(i==j)
{
max = min = a[i];
}
else
{
if(i == j-1)
{
if(a[i] <a[j])
{
max = a[j];

min = a[i];
}
else
{
max = a[i];
min = a[j];
}
}
else
{
mid = (i+j)/2;
maxmin(i, mid);
max1 = max; min1 = min;
maxmin(mid+1, j);
if(max <max1)
max = max1;
if(min > min1)
min = min1;
}
}
}
void main ()
{
int i, num;
clrscr();
printf ("Enter the size of the array : ");
scanf ("%d",&num);
printf ("Enter the elements of the array : ");
for (i=1;i<=num;i++)
{
scanf ("%d",&a[i]);
}
max = a[0];
min = a[0];
maxmin(1, num);
printf ("Miximum element in the array is : %d\n", min);
printf ("Maximum element in the array is : %d\n", max);
getch();
}

Potrebbero piacerti anche