Skip to content

Commit 8176e6c

Browse files
Add files via upload
C implementation of some popular Algorithms and code to find their time complexity
1 parent 2324513 commit 8176e6c

7 files changed

+485
-0
lines changed

DnC_Binary and linear search.c

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
=================================================================================================
2+
TIME COMPLEXITY
3+
Binary search and Linear Search
4+
5+
#include<stdio.h>
6+
#include<time.h>
7+
8+
int linearsearch(int A[],int el,int n) //Funtion to perform linear search
9+
{
10+
int i,flag=0;
11+
for(i=0;i<n;i++)
12+
{
13+
if(A[i]==el) //Iterating over each element and searching
14+
{ //for desired element to be searched
15+
flag=1;
16+
break;
17+
}
18+
}
19+
return flag;
20+
}
21+
int binarysearch(int A[],int n,int el) //Function to perform binary search
22+
{
23+
int l,u,mid,flag=0;
24+
l=0;
25+
u=n-1;
26+
while(l<u)
27+
{
28+
mid=(l+u)/2; //finding middle term
29+
if(el>A[mid]) //dividing problem into smaller problems
30+
l=mid+1; //first half array first element to middle
31+
else if (el<A[mid])
32+
u=mid-1; //second half array first element to middle
33+
else
34+
{
35+
flag=1;
36+
break;
37+
}
38+
}
39+
}
40+
41+
42+
int main()
43+
{
44+
int n,i,el,flag,fg;
45+
clock_t start,end,st,et;
46+
double time,t_ime;
47+
printf("Enter the number of elements in the array: ");
48+
scanf("%d",&n);
49+
int A[n];
50+
for(i=0;i<n;i++)
51+
{
52+
printf("Enter the %d element of the array: ",i+1);
53+
scanf("%d",&A[i]);
54+
}
55+
printf("Enter the element you want to search in the array: ");
56+
scanf("%d",&el);
57+
start=clock(); //start clock time
58+
flag=linearsearch(A,el,n); //function calling inear search
59+
end=clock(); //end clock time
60+
time = (double)(start-end)/CLOCKS_PER_SEC;
61+
st=clock(); //start clock time
62+
fg=binarysearch(A,n,el); //function calling Binary search
63+
et=clock(); //end clock time
64+
t_ime=(double)(end-start)/CLOCKS_PER_SEC;
65+
if(flag==1)
66+
printf("The element exists in the array");
67+
else
68+
printf("The element does not exist in the array\n");
69+
printf("Time taken for linear search is :%f\n",time);
70+
printf("Time taken by the process in seconds is : %f\n",t_ime);
71+
return 0;
72+
}
73+
74+
======================================================================================================

DnC_Binary search.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
================================================================================================
2+
DIVIDE AND CONQUER
3+
Binary Search Time complexity
4+
5+
#include<stdio.h>
6+
#include<time.h>
7+
8+
int binarysearch(int A[],int n,int el) //Function to perform binary search
9+
{
10+
int l,u,mid,flag=0;
11+
l=0;
12+
u=n-1;
13+
while(l<u)
14+
{
15+
mid=(l+u)/2; //finding middle term
16+
if(el>A[mid]) //dividing problem into smaller problems
17+
l=mid+1; //first half array first element to middle
18+
else if (el<A[mid])
19+
u=mid-1; //second half array first element to middle
20+
else
21+
{
22+
flag=1;
23+
break;
24+
}
25+
}
26+
}
27+
28+
int main()
29+
{
30+
clock_t start,end;
31+
double t; int i,n,flag,el;
32+
printf("Program for Binary Search!\n");
33+
printf("Enter a sorted array to successfully perform Binary Search. \n");
34+
printf("Enter the array size you want: ");
35+
scanf("%d",&n);
36+
int A[n];
37+
for(i=0;i<n;i++)
38+
{
39+
printf("Enter the %d element of the array: ",i+1);
40+
scanf("%d",&A[i]);
41+
}
42+
printf("Enter the element you want to search: ");
43+
scanf("%d",&el);
44+
printf("Performing the binary search\n");
45+
start=clock(); //start clock time
46+
flag=binarysearch(A,n,el); //function calling
47+
end=clock(); //end clock time
48+
if(flag==1)
49+
printf("Element does not exist in the array\n");
50+
else
51+
printf("Element exist in the array\n");
52+
t=(double)(end-start)/CLOCKS_PER_SEC;
53+
printf("Time taken by the process in seconds is : %f\n",t);
54+
return 0;
55+
}
56+
57+
=================================================================================================

DnC_Strassens's multiplication.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
=====================================================================
2+
/*Time Complexity
3+
STRASSEN'S MATRIX MULTIPLICATION*/
4+
5+
#include<stdio.h>
6+
#include<time.h>
7+
8+
int main()
9+
{
10+
int a[2][2], b[2][2], c[2][2], i, j;
11+
clock_t start,end;
12+
double time;
13+
int m1, m2, m3, m4 , m5, m6, m7;
14+
printf("Enter the 4 elements of first matrix: ");
15+
for(i = 0;i < 2; i++)
16+
for(j = 0;j < 2; j++)
17+
scanf("%d", &a[i][j]);
18+
printf("Enter the 4 elements of second matrix: ");
19+
for(i = 0; i < 2; i++)
20+
for(j = 0;j < 2; j++)
21+
scanf("%d", &b[i][j]);
22+
printf("\nThe first matrix is\n");
23+
for(i = 0; i < 2; i++)
24+
{
25+
printf("\n");
26+
for(j = 0; j < 2; j++)
27+
printf("%d\t", a[i][j]);
28+
}
29+
printf("\nThe second matrix is\n");
30+
for(i = 0;i < 2; i++)
31+
{
32+
printf("\n");
33+
for(j = 0;j < 2; j++)
34+
printf("%d\t", b[i][j]);
35+
}
36+
start = clock();
37+
m1= (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);
38+
m2= (a[1][0] + a[1][1]) * b[0][0];
39+
m3= a[0][0] * (b[0][1] - b[1][1]);
40+
m4= a[1][1] * (b[1][0] - b[0][0]);
41+
m5= (a[0][0] + a[0][1]) * b[1][1];
42+
m6= (a[1][0] - a[0][0]) * (b[0][0]+b[0][1]);
43+
m7= (a[0][1] - a[1][1]) * (b[1][0]+b[1][1]);
44+
c[0][0] = m1 + m4- m5 + m7;
45+
c[0][1] = m3 + m5;
46+
c[1][0] = m2 + m4;
47+
c[1][1] = m1 - m2 + m3 + m6;
48+
printf("\nAfter multiplication using Strassen's algorithm \n");
49+
for(i = 0; i < 2 ; i++)
50+
{
51+
printf("\n");
52+
for(j = 0;j < 2; j++)
53+
printf("%d\t", c[i][j]);
54+
}
55+
end = clock();
56+
time = (double)((start - end)/CLOCKS_PER_SEC);
57+
printf("Time taken by Strassen's Multiplication is : %f",time);
58+
return 0;
59+
}
60+
61+
====================================================================================

DnC_insertion sort.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
=======================================================================
2+
/*TIME COMPLEXITY
3+
Insertion sort*/
4+
5+
#include <stdio.h>
6+
#include<time.h>
7+
8+
int main()
9+
{
10+
int n, array[1000], c, d, t;
11+
clock_t start,end;
12+
double time;
13+
printf("Enter number of elements : ");
14+
scanf("%d", &n);
15+
for (c = 1; c <= n; c++)
16+
{
17+
printf("Enter the number %d : ", c);
18+
scanf("%d", &array[c]);
19+
}
20+
start=clock();
21+
for (c = 1000 ; c <= n - 1; c++)
22+
{
23+
d = c;
24+
while ( d > 0 && array[d-1] > array[d])
25+
{
26+
t = array[d];
27+
array[d] = array[d-1];
28+
array[d-1] = t;
29+
d--;
30+
}
31+
}
32+
end=clock();
33+
time = (double)((start-end)/CLOCKS_PER_SEC);
34+
printf("Sorted list in ascending order:\n");
35+
for (c = 0; c <= n - 1; c++)
36+
{
37+
printf("%d , ", array[c]);
38+
}
39+
printf("Time taken by insertion sort is %f",time);
40+
return 0;
41+
}
42+
43+
==============================================================================

DnC_mergesort.c

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
================================================================================================
2+
/*TIME COMPLEXITY
3+
Merge Sort*/
4+
5+
#include <stdio.h>
6+
#include <time.h>
7+
8+
void mergeSort(int [], int, int, int); //Merge sort function declaration
9+
void partition(int [],int, int); //Partition function declaration
10+
11+
int main()
12+
{
13+
int list[50];
14+
clock_t start,end;
15+
double time;
16+
int i, size;
17+
printf("Enter total number of elements:");
18+
scanf("%d", &size);
19+
printf("Enter the elements:\n");
20+
for(i = 0; i < size; i++)
21+
{
22+
scanf("%d", &list[i]);
23+
}
24+
start=clock(); //start time of clock
25+
partition(list, 0, size - 1); //calling of the partation function
26+
end=clock(); //end time of clock
27+
time = (double)(start-end)/CLOCKS_PER_SEC; //calculation of time taken
28+
printf("After merge sort:\n");
29+
printf("Time taken by merge sort is %f",time);
30+
for(i = 0;i < size; i++)
31+
{
32+
printf("%d ",list[i]);
33+
}
34+
return 0;
35+
}
36+
37+
void partition(int list[],int low,int high) //Partition function definition
38+
{
39+
int mid;
40+
if(low < high)
41+
{
42+
mid = (low + high) / 2;
43+
partition(list, low, mid); //Recurrsive calling
44+
partition(list, mid + 1, high); //Recurrsive calling
45+
mergeSort(list, low, mid, high); //Merge sort function calling
46+
}
47+
}
48+
49+
void mergeSort(int list[],int low,int mid,int high) //Merge sort function definition
50+
{
51+
int i, mi, k, lo, temp[50];
52+
lo = low;
53+
i = low;
54+
mi = mid + 1;
55+
while ((lo <= mid) && (mi <= high))
56+
{
57+
if (list[lo] <= list[mi])
58+
{
59+
temp[i] = list[lo];
60+
lo++;
61+
}
62+
else
63+
{
64+
temp[i] = list[mi];
65+
mi++;
66+
}
67+
i++;
68+
}
69+
if (lo > mid)
70+
{
71+
for (k = mi; k <= high; k++)
72+
{
73+
temp[i] = list[k];
74+
i++;
75+
}
76+
}
77+
else
78+
{
79+
for (k = lo; k <= mid; k++)
80+
{
81+
temp[i] = list[k];
82+
i++;
83+
}
84+
}
85+
86+
for (k = low; k <= high; k++)
87+
{
88+
list[k] = temp[k];
89+
}
90+
}
91+
92+
===============================================================================================

0 commit comments

Comments
 (0)