-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.c
46 lines (36 loc) · 896 Bytes
/
12.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
void linearSearch(int arr[], int element, int size){
for(int i =0 ; i<size; i++){
if(arr[i]==element){
printf("Element found at index %d\n",i);
break;
}
}
}
int binarySearch(int arr[], int element, int size){
int low = 0;
int high = size-1;
int mid;
while(low<=high){
mid = (high + low)/2;
if(element==arr[mid]){
printf("Element is found at index %d ",mid);
return mid;
}
if(element>arr[mid]){
low = mid+1;
}
else{
high = mid-1;
}
}
return -1;
}
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(arr)/sizeof(int);
binarySearch(arr,5,size);
// linearSearch(arr,4,size); time comlplexity is O(n)
return 0;
}