-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
45 lines (37 loc) · 1.1 KB
/
BinarySearch.cpp
File metadata and controls
45 lines (37 loc) · 1.1 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int devideConquer(vector<int> tempVec, int lowIndex, int highIndex, int num)
{
int index ;
index = (highIndex + lowIndex)/2;
if (tempVec[index] == num )
{
cout << "Element is present at index: " << index << endl;
return tempVec[index];
}
else if ((highIndex - lowIndex <= 1 ) && (tempVec[highIndex] != num) && (tempVec[lowIndex] != num))
{
cout << num << " Element is not present In This Array." << endl;
return false;
}
else if ((tempVec[index] != num) && (num > tempVec[index] ))
{
lowIndex = index;
return devideConquer(tempVec, lowIndex, highIndex, num );
}
else if ((tempVec[index] != num) && (num < tempVec[index]))
{
highIndex = index;
return devideConquer(tempVec, lowIndex, highIndex, num );
}
}
int main()
{
vector<int> tempVec = {3,3,3,3,3,3,3, -2, 4}; int num = -2;
int lowIndex = 0, highIndex = tempVec.size(), tempIndex;
sort(tempVec.begin(),tempVec.end());
tempIndex = devideConquer(tempVec, lowIndex, highIndex, num );
return 0;
}