Skip to content

Commit 5c4373e

Browse files
author
=
committed
Insertions sorting added in Lab3
1 parent 7e96282 commit 5c4373e

File tree

5 files changed

+221
-0
lines changed

5 files changed

+221
-0
lines changed

Lab3/a.out

13.3 KB
Binary file not shown.

Lab3/insertion.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<iostream>
2+
#include<time.h>
3+
using namespace std;
4+
int main()
5+
{
6+
clock_t s,e;
7+
float t;
8+
int n,i,j,key;
9+
cout<<"Enter size of array you want: ";
10+
cin>>n;
11+
int A[n];
12+
for(i=0;i<n;i++)
13+
{
14+
cout<<"Enter "<<i+1<<" element of array"<<endl;
15+
cin>>A[i];
16+
}
17+
cout<<"Lets do Insertion sort"<<endl;
18+
s=clock();
19+
for(i=0;i<n;i++)
20+
{
21+
key = A[i];
22+
j=i-1;
23+
while(j>=0 && A[j]>key)
24+
{
25+
A[j+1] = A[j];
26+
j=j-1;
27+
}
28+
A[j+1]=key;
29+
}
30+
e=clock();
31+
t=float(e-s)/CLOCKS_PER_SEC;
32+
cout<<"Sorted Array is:"<<endl;
33+
for(i=0;i<n;i++)
34+
{
35+
cout<<i+1<<" Element is :"<<A[i]<<endl;
36+
}
37+
cout<<"Time taken for processing is: "<<t<<endl;
38+
}

Lab3/naivemultiplication.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include<stdio.h>
2+
int main()
3+
{
4+
int r1,c1,r2,c2,i,j,k;
5+
printf("Enter the number of rows and columns of first matrix\n");
6+
scanf("%d%d",&r1,&c1);
7+
int A[r1][c1];
8+
for(i=0;i<r1;i++)
9+
{
10+
for(j=0;j<c1;j++)
11+
{
12+
printf("Enter the %d row and %d column element of A matrix: ",i+1,j+1);
13+
scanf("%d",&A[i][j]);
14+
}
15+
}
16+
printf("The first matrix is\n");
17+
for(i=0;i<r1;i++)
18+
{
19+
for(j=0;j<c1;j++)
20+
{
21+
printf("%d\t",A[i][j]);
22+
}
23+
printf("\n");
24+
}
25+
printf("Enter the number of rows and columns of second matrix\n");
26+
scanf("%d%d",&r2,&c2);
27+
int B[r2][c2];
28+
for(i=0;i<r2;i++)
29+
{
30+
for(j=0;j<c2;j++)
31+
{
32+
printf("Enter the %d row and %d column element of B matrix: ",i+1,j+1);
33+
scanf("%d",&B[i][j]);
34+
}
35+
}
36+
printf("The second matrix is\n");
37+
for(i=0;i<r2;i++)
38+
{
39+
for(j=0;j<c2;j++)
40+
{
41+
printf("%d\t",B[i][j]);
42+
}
43+
printf("\n");
44+
}
45+
printf("For the matrix multiplication process, we need number of columns of A matrix equal to the number of rows of B matrix\n");
46+
if(c1==r2)
47+
{
48+
int C[r1][c2];
49+
printf("Matrix multiplication process is possible for AxB\n");
50+
for(i=0;i<r1;i++)
51+
{
52+
for(j=0;j<c2;j++)
53+
{
54+
C[i][j]=0;
55+
for(k=0;k<c1;k++)
56+
{
57+
C[i][j]=C[i][j]+A[i][k]*B[k][j];
58+
}
59+
}
60+
}
61+
printf("The muliplicative matrix is: \n");
62+
for(i=0;i<r1;i++)
63+
{
64+
for(j=0;j<c2;j++)
65+
{
66+
printf("%d\t",C[i][j]);
67+
}
68+
printf("\n");
69+
}
70+
}
71+
else
72+
{
73+
printf("Matrix multiplication operation is not possible\n");
74+
}
75+
return 0;
76+
}

Lab3/quick.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<stdio.h>
2+
#include<time.h>
3+
///////////////////////////
4+
void swap(int *x,int *y)
5+
{
6+
int temp;
7+
temp = *x;
8+
*x = *y;
9+
*y =temp;
10+
}
11+
////////////////////////////
12+
int partition(int A[],int low,int high)
13+
{
14+
int i,j,pivot;
15+
pivot = A[high];
16+
i=low-1;
17+
for(j=low;j<=high-1;j++)
18+
{
19+
if(A[j]<=pivot)
20+
{
21+
i++;
22+
swap(&A[i],&A[j]);
23+
}
24+
}
25+
swap(&A[i+1],&A[high]);
26+
return (i+1);
27+
}
28+
//////////////////////////////
29+
void quicksort(int A[],int low,int high)
30+
{
31+
int pi;
32+
if (low<high)
33+
{
34+
pi = partition(A,low,high);
35+
quicksort(A,low,pi-1);
36+
quicksort(A,pi+1,high);
37+
}
38+
}
39+
//////////////////////////
40+
int main()
41+
{
42+
clock_t start,end;
43+
double time;
44+
int n,i;
45+
printf("Enter the number of elements in the array: ");
46+
scanf("%d",&n);
47+
int A[n];
48+
for(i=0;i<n;i++)
49+
{
50+
printf("Enter the %d element of the array: ",i+1);
51+
scanf("%d",&A[i]);
52+
}
53+
printf("Lets do the quick sort of given array\n");
54+
start=clock();
55+
quicksort(A,0,n-1);
56+
end=clock();
57+
time = (double)((start-end)/CLOCKS_PER_SEC);
58+
printf("The sorted array is:\n");
59+
for(i=0;i<n;i++)
60+
{
61+
printf("The %d element of the sorted array is: %d\n",i+1,A[i]);
62+
}
63+
printf("Time taken for the quick sort is: %f\n",time);
64+
return 0;
65+
}

Lab3/strmatrix.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<stdio.h>
2+
int main()
3+
{
4+
int A[2][2],B[2][2],C[2][2],i,j,p1,p2,p3,p4,p5,p6,p7;
5+
printf("Enter the elements of matrix A\n");
6+
for(i=0;i<2;i++)
7+
{
8+
for(j=0;j<2;j++)
9+
{
10+
scanf("%d",&A[i][j]);
11+
}
12+
}
13+
printf("Enter the elements of matrix B\n");
14+
for(i=0;i<2;i++)
15+
{
16+
for(j=0;j<2;j++)
17+
{
18+
scanf("%d",&B[i][j]);
19+
}
20+
}
21+
p1=A[0][0]*(B[0][1]-B[1][1]);
22+
p2=(A[0][0]+A[0][1])*B[1][1];
23+
p3=(A[1][0]+A[1][1])*B[0][0];
24+
p4=A[1][1]*(B[1][0]-B[0][0]);
25+
p5=(A[0][0]+A[1][1])*(B[0][0]+B[1][1]);
26+
p6=(A[0][1]-A[1][1])*(B[1][0]+B[1][1]);
27+
p7=(A[0][0]-A[1][0])*(B[0][0]+B[0][1]);
28+
C[0][0]=p5+p4-p2+p6;
29+
C[0][1]=p1+p2;
30+
C[1][0]=p3+p4;
31+
C[1][1]=p1+p5-p3-p7;
32+
printf("Printing the array C\n");
33+
for(i=0;i<2;i++)
34+
{
35+
for(j=0;j<2;j++)
36+
{
37+
printf("%d\t",C[i][j]);
38+
}
39+
printf("\n");
40+
}
41+
return 0;
42+
}

0 commit comments

Comments
 (0)