Skip to content

Commit 5c360c6

Browse files
Implementation of basic C codes with their time complexity
0 parents  commit 5c360c6

File tree

5 files changed

+369
-0
lines changed

5 files changed

+369
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
=======================================================================================================
2+
Time calculation for bubble sort and selection sort.
3+
4+
#include<stdio.h>
5+
#include<time.h> //To include clock function (used to calculate time taken by function
6+
7+
8+
void bubble_sort(int A[],int n) //Function to perform bubble sort
9+
{
10+
int i,j,temp;
11+
for(i=0;i<n-1;i++)
12+
{
13+
for(j=0;j<n-i-1;j++)
14+
{
15+
if(A[j]>A[j+1])
16+
{
17+
temp = A[j+1];
18+
A[j+1] = A[j];
19+
A[j] = temp;
20+
}
21+
}
22+
}
23+
for(i=0;i<n;i++) //To print sorted array
24+
{
25+
printf("%d",A[i]," , ");
26+
printf(" , ");
27+
}
28+
}
29+
30+
void selection_sort(int A[],int n) //Function to perform selection sort
31+
{
32+
int i,j,temp;
33+
for(i=0;i<n-1;i++)
34+
{
35+
for(j=i+1;j<n;j++)
36+
{
37+
if(A[j]<A[i])
38+
{
39+
temp = A[i];
40+
A[i] = A[j];
41+
A[j] = temp;
42+
}
43+
}
44+
}
45+
for(i=0;i<n;i++) //To print sorted array
46+
{
47+
printf("%d",A[i]);
48+
printf(" , ");
49+
}
50+
}
51+
52+
int main()
53+
{
54+
int n,i;
55+
clock_t st,en;
56+
double t;
57+
printf("Enter size of the array: ");
58+
scanf("%d",&n);
59+
int A[n];
60+
for(i=0;i<n;i++) //Input of array
61+
{
62+
printf("\nEnter the %d element of the array: ",i+1);
63+
scanf("%d",&A[i]);
64+
}
65+
int B[n];
66+
for(i=0;i<n;i++) //Making 2 copies of entered array – one for bubble sort
67+
B[i]=A[i]; // and another for selection sort
68+
st = clock(); //starting of clock
69+
bubble_sort(A,n); //calling bubble sort function
70+
en= clock(); //ending of clock 1
71+
t= (double)(en-st)/CLOCKS_PER_SEC; //time elapsed for bubble sort
72+
printf("Time taken for Bubble sort is :%f \n",t);
73+
st = clock(); //clock for selection sort
74+
selection_sort(B,n); //function calling
75+
en=clock(); //ending of clock 2
76+
t= (double)(en-st)/CLOCKS_PER_SEC; //time elapsed for selection sort
77+
printf("Time taken for Selection sort is :%f \n",t);
78+
return 0;
79+
}
80+
81+
82+
======================================================================================================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
=======================================================================================================
2+
Time calculation for bubble sort and selection sort.
3+
4+
#include<stdio.h>
5+
#include<time.h> //To include clock function (used to calculate time taken by function
6+
7+
8+
void bubble_sort(int A[],int n) //Function to perform bubble sort
9+
{
10+
int i,j,temp;
11+
for(i=0;i<n-1;i++)
12+
{
13+
for(j=0;j<n-i-1;j++)
14+
{
15+
if(A[j]>A[j+1])
16+
{
17+
temp = A[j+1];
18+
A[j+1] = A[j];
19+
A[j] = temp;
20+
}
21+
}
22+
}
23+
for(i=0;i<n;i++) //To print sorted array
24+
{
25+
printf("%d",A[i]," , ");
26+
printf(" , ");
27+
}
28+
}
29+
30+
void selection_sort(int A[],int n) //Function to perform selection sort
31+
{
32+
int i,j,temp;
33+
for(i=0;i<n-1;i++)
34+
{
35+
for(j=i+1;j<n;j++)
36+
{
37+
if(A[j]<A[i])
38+
{
39+
temp = A[i];
40+
A[i] = A[j];
41+
A[j] = temp;
42+
}
43+
}
44+
}
45+
for(i=0;i<n;i++) //To print sorted array
46+
{
47+
printf("%d",A[i]);
48+
printf(" , ");
49+
}
50+
}
51+
52+
int main()
53+
{
54+
int n,i;
55+
clock_t st,en;
56+
double t;
57+
printf("Enter size of the array: ");
58+
scanf("%d",&n);
59+
int A[n];
60+
for(i=0;i<n;i++) //Input of array
61+
{
62+
printf("\nEnter the %d element of the array: ",i+1);
63+
scanf("%d",&A[i]);
64+
}
65+
int B[n];
66+
for(i=0;i<n;i++) //Making 2 copies of entered array � one for bubble sort
67+
B[i]=A[i]; // and another for selection sort
68+
st = clock(); //starting of clock
69+
bubble_sort(A,n); //calling bubble sort function
70+
en= clock(); //ending of clock 1
71+
t= (double)(en-st)/CLOCKS_PER_SEC; //time elapsed for bubble sort
72+
printf("Time taken for Bubble sort is :%f \n",t);
73+
st = clock(); //clock for selection sort
74+
selection_sort(B,n); //function calling
75+
en=clock(); //ending of clock 2
76+
t= (double)(en-st)/CLOCKS_PER_SEC; //time elapsed for selection sort
77+
printf("Time taken for Selection sort is :%f \n",t);
78+
return 0;
79+
}
80+
81+
82+
======================================================================================================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
========================================================================================================
2+
/*TIME COMPLEXITY
3+
NORMAL MULTIPLICATION METHOD*/
4+
5+
#include<stdio.h>
6+
#include<time.h>
7+
8+
int main()
9+
{
10+
int r1,c1,r2,c2,i,j,k;
11+
clock_t start,end;
12+
double time;
13+
14+
printf("Enter the number of rows and columns of first matrix\n");
15+
scanf("%d%d",&r1,&c1);
16+
int A[r1][c1];
17+
for(i=0;i<r1;i++)
18+
{
19+
for(j=0;j<c1;j++)
20+
{
21+
printf("Enter the %d row and %d column element of A matrix: ",i+
22+
1,j+1);
23+
scanf("%d",&A[i][j]);
24+
}
25+
}
26+
printf("Enter the number of rows and columns of second matrix\n");
27+
scanf("%d%d",&r2,&c2);
28+
int B[r2][c2];
29+
for(i=0;i<r2;i++)
30+
{
31+
for(j=0;j<c2;j++)
32+
{
33+
printf("Enter the %d row and %d column element of B matrix: ",i+
34+
1,j+1);
35+
scanf("%d",&B[i][j]);
36+
}
37+
}
38+
printf("The first matrix is\n");
39+
for(i=0;i<r1;i++)
40+
{
41+
for(j=0;j<c1;j++)
42+
{
43+
printf("%d\t",A[i][j]);
44+
}
45+
printf("\n");
46+
}
47+
48+
printf("The second matrix is\n");
49+
for(i=0;i<r2;i++)
50+
{
51+
for(j=0;j<c2;j++)
52+
{
53+
printf("%d\t",B[i][j]);
54+
}
55+
printf("\n");
56+
}
57+
printf("For the matrix multiplication process, the number of columns of A matrix
58+
must be equal to the number of rows of B matrix\n");
59+
start=clock();
60+
if(c1==r2)
61+
{
62+
int C[r1][c2];
63+
printf("Matrix multiplication process is possible for AxB\n");
64+
for(i=0;i<r1;i++)
65+
{
66+
for(j=0;j<c2;j++)
67+
{
68+
C[i][j]=0;
69+
for(k=0;k<c1;k++)
70+
{
71+
C[i][j]=C[i][j]+A[i][k]*B[k][j];
72+
}
73+
}
74+
}
75+
printf("The muliplicative matrix is: \n");
76+
for(i=0;i<r1;i++)
77+
{
78+
for(j=0;j<c2;j++)
79+
{
80+
printf("%d\t",C[i][j]);
81+
}
82+
printf("\n");
83+
}
84+
}
85+
else
86+
{
87+
printf("Matrix multiplication operation is not possible\n");
88+
}
89+
end=clock();
90+
time = (double)((start -end)/CLOCKS_PER_SEC);
91+
printf("Time taken by naive multiplication method is : %f",time);
92+
return 0;
93+
}
94+
95+
====================================================================================================
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
========================================================================================
2+
TIME COMPLEXITY
3+
Patter searching in a given text field
4+
5+
#include<stdio.h>
6+
#include<time.h>
7+
8+
int search(char A[],char B[],int n,int m) //function to search position of pattern
9+
{ //in the entered text
10+
int i,j,pos=-1;
11+
for(i=0;i<n-m;i++)
12+
{
13+
if(A[i]==B[0])
14+
{
15+
j=0;
16+
while(j<m && A[i+j]==B[j])
17+
j=j+1;
18+
}
19+
if(j==m)
20+
pos=i;
21+
}
22+
return pos;
23+
}
24+
25+
int main()
26+
{
27+
int n,m,pos;
28+
clock_t start,end;
29+
double t;
30+
printf("Enter the respective sizes of Text \n");
31+
scanf("%d",&n);
32+
printf("Size of pattern array such that text has more size \n");
33+
scanf("%d",&m);
34+
if (n>=m) //text should be greater than pattern to search
35+
{ //for sucessfully
36+
char TEXT[n],PATTERN[m];
37+
printf("Enter the text string\n");
38+
scanf("%s",TEXT);
39+
printf("Enter the pattern string\n");
40+
scanf("%s",PATTERN);
41+
start=clock(); //clock starting
42+
pos = search(TEXT,PATTERN,n,m); //function calling
43+
end=clock(); //clock ending
44+
t=(double)(start-end)/CLOCKS_PER_SEC;
45+
printf("The element is found at %d index\n",pos);
46+
printf("The time taken is %f\n",t);
47+
}
48+
else
49+
{
50+
printf("The size of pattern cannot be more than text!!");
51+
}
52+
return 0;
53+
}
54+
55+
=================================================================================================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
========================================================================================
2+
TIME COMPLEXITY
3+
Patter searching in a given text field
4+
5+
#include<stdio.h>
6+
#include<time.h>
7+
8+
int search(char A[],char B[],int n,int m) //function to search position of pattern
9+
{ //in the entered text
10+
int i,j,pos=-1;
11+
for(i=0;i<n-m;i++)
12+
{
13+
if(A[i]==B[0])
14+
{
15+
j=0;
16+
while(j<m && A[i+j]==B[j])
17+
j=j+1;
18+
}
19+
if(j==m)
20+
pos=i;
21+
}
22+
return pos;
23+
}
24+
25+
int main()
26+
{
27+
int n,m,pos;
28+
clock_t start,end;
29+
double t;
30+
printf("Enter the respective sizes of Text \n");
31+
scanf("%d",&n);
32+
printf("Size of pattern array such that text has more size \n");
33+
scanf("%d",&m);
34+
if (n>=m) //text should be greater than pattern to search
35+
{ //for sucessfully
36+
char TEXT[n],PATTERN[m];
37+
printf("Enter the text string\n");
38+
scanf("%s",TEXT);
39+
printf("Enter the pattern string\n");
40+
scanf("%s",PATTERN);
41+
start=clock(); //clock starting
42+
pos = search(TEXT,PATTERN,n,m); //function calling
43+
end=clock(); //clock ending
44+
t=(double)(start-end)/CLOCKS_PER_SEC;
45+
printf("The element is found at %d index\n",pos);
46+
printf("The time taken is %f\n",t);
47+
}
48+
else
49+
{
50+
printf("The size of pattern cannot be more than text!!");
51+
}
52+
return 0;
53+
}
54+
55+
=================================================================================================

0 commit comments

Comments
 (0)