-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode6-binarySearchPractice.cpp
More file actions
57 lines (49 loc) · 1.2 KB
/
code6-binarySearchPractice.cpp
File metadata and controls
57 lines (49 loc) · 1.2 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
51
52
53
54
55
56
57
#include <iostream>
using namespace std;
// binary search is the search which is APPLY ON SORTED ARRAY
// CONCEPT LOOK FOR START , MIDDLE , END .
// SEARCH FOR THE KEY IN THE EITHER OF TWO Parts of the array after comparing the middle element .
// to run cpp program control + option + N ;
/// SUMMARY OF THE Binary
int binarySearch(int arr[], int n, int key)
{
int start = 0;
int end = n - 1;
while (start < end)
{
int middle = (start + end) / 2;
if (key == arr[middle])
{
return middle;
}
else if (key < arr[middle])
{
end = middle - 1;
return middle;
}
else
{
start = middle + 1;
}
}
return -1;
}
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof(a) / sizeof(int);
int key;
cout << "n is " << n << endl;
cout << "Enter the number , which you want to find in the given array ..... " << endl;
cin >> key;
int found = binarySearch(a, n, key);
if (found == -1)
{
cout << " key is not found" << endl;
}
else
{
cout << " found key is " << found << endl;
}
}
// all doubts clear here