-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
61 lines (52 loc) · 853 Bytes
/
BinarySearch.cpp
File metadata and controls
61 lines (52 loc) · 853 Bytes
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
#include <iostream>
using namespace std;
int main(){
int count;
cin>>count;
int input[count];
int min=2147483647;
for(int i=0;i<count;i++){
cin>>input[i];
}
cout<<"--------------------"<<endl;
for(int i=0;i<count;i++){
int min=i;
for(int j=i+1;j<count;j++){
if(input[min]>input[j]){
min=j;
}
}
int tmp=input[i];
input[i]=input[min];
input[min]=tmp;
for(int x=0;x<count;x++){
cout<<input[x]<<" ";
}
cout<<endl;
}
cout<<"--------------------"<<endl;
for(int i=0;i<count;i++){
cout<<input[i]<<" ";
}
cout<<"\n--------------------"<<endl;
int search;
cin>>search;
int l=0,r=count-1,n;
while(true){
n=(l+r)/2;
if(input[n]==search){
break;
}
else if(l>=r){
cout<<"-1";
return 0;
}
else if(input[n]>search){
r=n-1;
}
else{
l=n+1;
}
}
cout<<"["<<n<<"]"<<search;
}