-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
43 lines (32 loc) · 1002 Bytes
/
Copy pathbinarySearch.cpp
File metadata and controls
43 lines (32 loc) · 1002 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
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 10
using namespace std;
int main() {
int arr_search[MAX_SIZE], i, element;
int f = 0, r = MAX_SIZE, mid;
cout << "Simple C++ Binary Search Example - Array\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Searching : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_search[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_search[i];
}
cout << "\nEnter Element to Search : ";
cin>>element;
while (f <= r) {
mid = (f + r) / 2;
if (arr_search[mid] == element) {
cout << "\nSearch Element : " << element << " : Found : Position : " << mid + 1 << ".\n";
break;
} else if (arr_search[mid] < element)
f = mid + 1;
else
r = mid - 1;
}
if (f > r)
cout << "\nSearch Element : " << element << " : Not Found \n";
getch();
}