-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary-search.cpp
More file actions
executable file
·32 lines (30 loc) · 1.06 KB
/
binary-search.cpp
File metadata and controls
executable file
·32 lines (30 loc) · 1.06 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
#include
using namespace std;
int binarySearch(int arr[], int p, int r, int num) {
if (p <= r) {
int mid = (p + r)/2;
if (arr[mid] == num)
return mid ;
if (arr[mid] > num)
return binarySearch(arr, p, mid-1, num);
if (arr[mid] < num)
return binarySearch(arr, mid+1, r, num);
}
return -1;
}
int main(void) {
int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40};
int n = sizeof(arr)/ sizeof(arr[0]);
int num;
cout << "Enter the number to search: \n";
cin >> num;
int index = binarySearch (arr, 0, n-1, num);
if(index == -1){
cout<< num <<" is not present in the array";
}else{
cout<< num <<" is present at index "<< index <<" in the array";
}
return 0;
}
/*
Binary search: This method is efficient for searching through sorted arrays by repeatedly dividing the search interval in half. The time complexity of binary search is O(log n) in the worst case, where n is the number of elements in the array. The space complexity is O(1), as it does not use any additional data structures.*/