-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbserch.cpp
78 lines (76 loc) · 1.42 KB
/
bserch.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
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using namespace std;
int binarysrech(int a[],int n,int key){
int s=0;
int e=n-1;
int mid=(s+e)/2;
while (s<=e)
{
if (a[mid]==key)
{
return mid;
break;
}
if (a[mid]<key)
{
s=mid+1;
}else
{
e=mid-1;
}
mid=(s+e)/2;
}
return -1;
}
int main(){
int even[6]={1,2,3,4,5,6};
int odd[5]={1,5,10,19,27};
cout<<binarysrech(even,6,5)<<endl;;
cout<<binarysrech(odd,5,19);
return 0;
}
//optimized
int binarysrech(int a[],int n,int key){
int s=0;
int e=n-1;
int mid=s+(e-s)/2;
while (s<=e)
{
if (a[mid]==key)
{
return mid;
break;
}
if (a[mid]<key)
{
s=mid+1;
}else
{
e=mid-1;
}
mid=s+(e-s)/2;
}
return -1;
}
//leetcode optimized from 18%beats to 62%
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int s =0;
int e = nums.size();
if(target>nums[e-1]){
return e;
}
while(s<=e){
int mid = (s+e)/2;
if(nums[mid]==target){
return mid;
}else if(nums[mid]>target){
e = mid - 1;
}else {
s = mid +1;
}
}
return s;
}
};