-
Notifications
You must be signed in to change notification settings - Fork 63
/
binary__search.cpp
67 lines (52 loc) ยท 1.06 KB
/
binary__search.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
using namespace std;
//binary search --> key = 27, {8,10,12,21,27,34,42}
int binarySearch(int arr[], int n , int key)
{
int s=0;
int e=n;
while(s<=e){
int mid=(s+e)/2;
if(arr[mid]==key)
{
return mid;
}
else if (arr[mid]>key)
{
e=mid-1;
}
else
{
s=mid+1;
}
}
return -1;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0; i<n;i++)
{
cin>>arr[i];
}
int key;
cin>>key;
cout<<binarySearch(arr,n, key);
return 0;
}
/*
time complexity
after first iteration, lenght of array -->n
after second iteration, lenght of array becomes n/2
after the k iterations, length of array will becomes n/2^k
let the array becomes 1 after k iterations
n/2^k=1
n=2^k
log2(n)=log2(2^k)
log2(n)=k log 2(2)
k=log2(n)
therefore, time complexity will be O(log2(n)), this is less than O(n), as this is of linear search
hence, the binary search is less time consuimg then binary search
*/