0% found this document useful (0 votes)
34 views

DAA Assignment 1: by B.Teja Sai (18VF1M3310)

This document contains 6 programs that demonstrate different sorting algorithms: 1. Selection sort 2. Insertion sort 3. Binary search 4. Quick sort 5. Merge sort 6. Matrix multiplication using Strassen's algorithm The programs provide code snippets for each sorting algorithm and matrix multiplication method with comments explaining what each section of code is doing.

Uploaded by

Téjã Sâì
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

DAA Assignment 1: by B.Teja Sai (18VF1M3310)

This document contains 6 programs that demonstrate different sorting algorithms: 1. Selection sort 2. Insertion sort 3. Binary search 4. Quick sort 5. Merge sort 6. Matrix multiplication using Strassen's algorithm The programs provide code snippets for each sorting algorithm and matrix multiplication method with comments explaining what each section of code is doing.

Uploaded by

Téjã Sâì
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

DAA Assignment 1

By B.Teja Sai(18VF1M3310)

1. Program to sort elements using selection sort


#include<stdio.h>
int selection(int [], int);
int partition(int[],int,int,int);

int partition(int a[],int k,int n,int loc)


{
int min,i;
min=a[k];
loc=k;
for(i=k;i<n;i++)
{
if(a[i]<min)
{
min=a[i];
loc=i;
}}
return loc;
}
int selection(int a[],int n)
{
int loc=-1;
for(int k=0;k<n-1;k++)
{
int j=partition(a,k,n,loc);
int temp = a[k];
a[k] = a[j];
a[j] = temp;
}
printf("The sorted list is:\n");
for(int k=0;k<n;k++)
printf("%d ",a[k]);
}
int main()
{
int a[20],n,i;
printf("Enter no of num; ");
scanf("%d", &n);
printf("Enter numbers; \n");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
selection(a,n);
return 0;
}

2.Program to sort elements using insertion sort


#include<stdio.h>
void isort(int a[], int n);
int main()
{
int a[20], i, n;
printf("Enter no of elements: \n");
scanf("%d", &n);
printf("Enter the elements: \n");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
isort(a,n);
printf("\nInsertion sort\n");
printf("Sorted elements are:\n");
for(i=0;i<n;i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
void isort(int a[], int n)
{
int temp, i, j;
for (i = 1 ; i <= n - 1; i++)
j = i;
while ( j > 0 && a[j-1] > a[j])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
j--;
}
}

3. Program for binary search


#include<stdio.h>
int main()
{
int bin(int [],int,int);
int n,i,a[100],x,res;
printf("No of Elements: \n");
scanf("%d", &n);
printf("\nEnter Elements: \n");

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

printf("\nEnter element to search:");


scanf("%d", &x);

res=bin(a,n,x);

if(res!=-1)
printf("\nElement found at position: %d \n",
res+1);
else
printf("\nElement is not found....!!!\n");

return 0;
}

int bin(int a[],int n,int x)


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

4. Program to sort elements using quick sort


#include<stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);

int main()
{
int a[50],n,i;
printf("How many elements:");
scanf("%d", &n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
quick_sort(a,0,n-1);
printf("\nArray after sorting:");

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

return 0;
}

void quick_sort(int a[],int m,int p)


{
int j;
if(m<p)
{
j=partition(a,m,p);
quick_sort(a,m,j-1);
quick_sort(a,j+1,p);
}
}

int partition(int a[],int m,int p)


{
int v,i,j,temp;
v=a[m];
i=m;
j=p+1;
do
{
do
i=i+1;
while(a[i]<v&&i<=p);
do
j=j-1;
while(a[j]>v);

if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
while(i<j);
a[m]=a[j];
a[j]=v;
return(j);
}

5.Program to sort element using merge sort.


#include<stdio.h>
void mergesort(int a[], int low,int high);
void merge(int a[],int low,int mid,int high);
int main()
{
int n, a[20], low , high;
printf("Enter no of elements: \n");
scanf("%d", &n);
printf("enter the elements: \n");
for(low=0;low<n;low++)
{
scanf("%d", &a[low]);
}
mergesort(a,0,n-1);
printf("\nSorted array is :\n");
for(low=0;low<n;low++)
printf("%d\n", a[low]);
return 0;
}

void mergesort(int a[],int low,int high)


{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low, mid);
mergesort(a,mid+1, high);
merge(a,low,mid,high);
}
}

void merge(int a[],int low,int mid,int high)


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

6.Program for STRASSEN MATRIX


MULTIPLICATION
#include<stdio.h>
int main()
{
int a[2][2],b[2][2],c[2][2];
int m1,m2,m3,m4,m5,m6,m7,i,j;
printf("Matrix Multiplication Strassrn's method
(2x2)\n");
printf("Enter the elements of Matrix 1:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of Matrix 2:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d", &b[i][j]);
}
}
printf("\nFirst matrix is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
printf("\nSecond matrix is\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d", b[i][j]);
}
printf("\n");
}

m1= (a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1+m3-m2+m6;

printf("\nProduct of both is:\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d",c[i][j]);
}
printf("\n");
}
}

You might also like