-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_old.cpp
More file actions
43 lines (28 loc) · 1.09 KB
/
binary_search_old.cpp
File metadata and controls
43 lines (28 loc) · 1.09 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
#include <iostream>
using namespace std;
void binarySearch(int arr[], int target, int low_index, int high_index);
int main()
{
int arr[6] = {1,3,2,4,24,12};
int target=3, low_index=0, high_index=9;
binarySearch(arr, target, low_index, high_index);
return 0;
}
//void binarySearch(int arr[], int target, int low_index, int high_index){
//void binarySearch(int arr[6], int target, int low_index, int high_index){
void binarySearch(int *arr[], int target, int low_index, int high_index){
int mid_index = int((low_index+high_index)/2);
if (low_index>high_index){
cout << "Not in the list..." << endl;
}else {
if (arr[mid_index]==target){
cout << "Element " << target << " is present in " << mid_index << " index location (index starting from zero)." << endl;
}else {
if (arr[mid_index]>target){
binarySearch(arr,target, low_index, mid_index-1);
}else if (arr[mid_index]<target){
binarySearch(arr,target, mid_index+1, high_index);
}
}
}
}