-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrongBinarySearch.cpp
More file actions
50 lines (47 loc) · 1.33 KB
/
wrongBinarySearch.cpp
File metadata and controls
50 lines (47 loc) · 1.33 KB
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
#include <iostream> //BINARY SEARCH
using namespace std;
int main(){
int search( int[] );
int numSearch;
const int n;
cin>>n;
int array[n];
cout<<"Enter the elements of the array ";
for(int i = 0; i < n; i++){
cin>>array[i]>>" ";
}
endl;
cout<<"Enter the element you want to search "
cin>>numSearch;
search(array[n]);
}
int search( int searchArray[] ){
if( n % 2 == 0 ){ //array size is an even number
int half = (n/2);
if( numSearch < half ){
for(int i = 0; i < (n/2); i++){
if(array[i] == numSearch){
return array[i];
}
}
} else {
for(int i = (n/2); i < n; i++){
if(array[i] == numSearch){
return array[i];
}
}
} else {
int half = (n/2) + 1; //array size is and odd number
if( numSearch < half ){
for(int i = 0; i < (n/2); i++){
if(array[i] == numSearch){
return array[i];
}
}
} else {
for(int i = (n/2); i < n; i++){
if(array[i] == numSearch;
return array[i];
}
}
}