Skip to content

Commit 1d59ef1

Browse files
committed
initial commit
1 parent a41d4ee commit 1d59ef1

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

binary_searching.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
3+
int binarysearch(int arr[], int element, int size)
4+
{
5+
int low = 0, high = size - 1;
6+
int mid;
7+
while (low <= high)
8+
{
9+
mid = (low + high) / 2;
10+
if (element == arr[mid])
11+
{
12+
13+
return mid;
14+
}
15+
else if (element > arr[mid])
16+
{
17+
low = mid + 1;
18+
}
19+
else
20+
{
21+
high = mid - 1;
22+
}
23+
}
24+
return -1;
25+
}
26+
27+
int main()
28+
{
29+
30+
int arr[50] = {1, 2, 4, 5, 66, 77};
31+
int element = 421321, size = 6;
32+
int search = binarysearch(arr, element, size);
33+
if (search == -1)
34+
{
35+
printf("Yur element %d was not found in the array\n", element);
36+
}
37+
else
38+
{
39+
printf("%d found at %d", element, search);
40+
}
41+
42+
return 0;
43+
}

linear_searching.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
3+
int Linearsearch(int arr[], int element, int size)
4+
{
5+
6+
for (int i = 0; i < size; i++)
7+
{
8+
9+
if (arr[i] == element)
10+
{
11+
12+
return i;
13+
}
14+
}
15+
return 1;
16+
}
17+
18+
int main()
19+
{
20+
21+
int arr[50] = {1, 3, 4, 78, 56, 78};
22+
int element = 4432, size = 6;
23+
int search = Linearsearch(arr, element, size);
24+
if (search == 1)
25+
{
26+
printf("Yur element %d was not found in the array\n",element);
27+
}
28+
else
29+
{
30+
printf("%d found at %d", element, search);
31+
}
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)