-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
64 lines (55 loc) · 1.22 KB
/
BinarySearch.cpp
File metadata and controls
64 lines (55 loc) · 1.22 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
58
59
60
61
62
63
64
#include <iostream>
using namespace std;
int binarySearch(int A[], int t, int n)
{
int start = 0;
int end = t-1;
while(start<=end)
{
int mid = (start+end)/2;
if(A[mid]==n)
{
return mid;
}
else {
if(n>A[mid])
{
start = mid+1;
}
else if(n<A[mid])
{
end = mid - 1;
}
}
}
}
int main()
{
int arr[5] = {1,3,5,4,2};
int temp, e;
for (int i=0; i<5; i++)
{
cout << arr[i] << endl;
}
for (int i=0; i<=5; i++)
{
for (int j=0; j<=5; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]= temp;
}
}
}
cout << "array after sorting: " << endl;
for (int i=0; i<5; i++)
{
cout << arr[i] << endl;
}
cout << "Enter the number you want to find: " << endl;
cin >> e;
cout << "The entered number is at index: " << binarySearch(arr,5,e) << endl;
return 0;
}