Skip to content

Commit 7f3f2b6

Browse files
author
Bhrigu Kansra
authored
Merge pull request #30 from kartikeytripathi/master
Add Binary Search in C++
2 parents 085325d + 8da9714 commit 7f3f2b6

File tree

3 files changed

+175
-16
lines changed

3 files changed

+175
-16
lines changed

contributors.md

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
# List of all contributors
22

3-
4-
1. [Bhrigu Kansra](https://github.com/kinetickansra)
5-
2. [Tathya Thaker](https://github.com/thetathya)
6-
3. [Aslan](https://github.com/impeccableaslan)
7-
4. [Naman Manchanda](https://github.com/namanmanchanda09)
8-
5. [Harsh Arora](https://github.com/aroraharsh010)
9-
6. [Ujjval Patel](https://github.com/Ujjval-Patel)
10-
7. [Shaguna Awasthi](https://github.com/Shagunaawasthi)
11-
8. [Joy Banerjee](https://github.com/joybanerjee08)
12-
9. [Ambika](https://github.com/Ambika55)
13-
10. [Shivam Yadav](https://github.com/shivamyadav2512)
14-
11. [Nikhil Arora](https://github.com/nikhilarora06
15-
16-
12. [Your Name](https://github.com/yourprofile)
17-
18-
3+
1. [Bhrigu Kansra](https://github.com/kinetickansra)
4+
2. [Tathya Thaker](https://github.com/thetathya)
5+
3. [Aslan](https://github.com/impeccableaslan)
6+
4. [Naman Manchanda](https://github.com/namanmanchanda09)
7+
5. [Harsh Arora](https://github.com/aroraharsh010)
8+
6. [Ujjval Patel](https://github.com/Ujjval-Patel)
9+
7. [Shaguna Awasthi](https://github.com/Shagunaawasthi)
10+
8. [Joy Banerjee](https://github.com/joybanerjee08)
11+
9. [Kartikey Tripathi](https://github.com/kartikeytripathi)

searching/binary_search.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Binary Search
3+
-----------
4+
A searching algorithm that finds the position of a target value
5+
within a sorted array.
6+
7+
Time complexity
8+
---------------
9+
O(log(N)), where N is the number of elements in the array.
10+
11+
Space complexity
12+
----------------
13+
O(1).
14+
*/
15+
16+
#include <iostream>
17+
#include <vector>
18+
19+
using namespace std;
20+
21+
int binarySearch(const int value, const vector<int>& sortedVect, const int low, const int high) {
22+
int mid = (low + high) / 2;
23+
24+
if (value == sortedVect[mid])
25+
return mid;
26+
else if (low <= high) {
27+
if (value < sortedVect[mid]) {
28+
// value must be between indices low and mid-1, if exists
29+
return binarySearch(value, sortedVect, low, mid-1);
30+
}
31+
else if (value > sortedVect[mid]) {
32+
// value must be between indices mid-1 and high, if exists
33+
return binarySearch(value, sortedVect, mid+1, high);
34+
}
35+
}
36+
37+
return -1;
38+
}
39+
40+
int main() {
41+
int value;
42+
cout << "Enter the value to search for : ";
43+
cin >> value;
44+
45+
int size;
46+
cout << "Enter the input size : ";
47+
cin >> size;
48+
49+
vector<int> inputVect(size); // supposedly sorted values
50+
cout << "Enter " << size << " integers in ascending order :\n";
51+
for (int& val: inputVect)
52+
cin >> val;
53+
54+
int index = binarySearch(value, inputVect, 0, size-1);
55+
cout << "\n";
56+
if (index != -1)
57+
cout << "Found " << value << " at position " << (index + 1) << "\n";
58+
else
59+
cout << "Either " << value << " is not present among the input values, or the values aren\'t sorted\n";
60+
61+
return 0;
62+
}

searching/ternary_search.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Ternary search
3+
--------------
4+
A searching algorithm that finds the position of a maximum(minimum) value
5+
within an unimodal array.
6+
7+
Time complexity
8+
---------------
9+
O(log(N)), where N is the number of elements in the array.
10+
11+
Space complexity
12+
----------------
13+
O(1).
14+
*/
15+
16+
#include <iostream>
17+
#include <vector>
18+
19+
using namespace std;
20+
21+
enum Pattern {
22+
ASCEND_THEN_DESCEND,
23+
DESCEND_THEN_ASCEND
24+
};
25+
26+
/*
27+
ternary_search
28+
-------------
29+
If the values first ascend and then descend, this function finds the position
30+
of the maximum value. Otherwise, if they first descend and then ascend, it
31+
finds the position of the minimum value.
32+
*/
33+
template <typename T>
34+
size_t ternary_search(const vector<T>& values, const Pattern& pattern) {
35+
// left and right are the edges of the interval of search
36+
size_t left = 0;
37+
size_t right = values.size() - 1;
38+
39+
bool changed = true;
40+
size_t mid1, mid2;
41+
while (right - left > 1 and changed) { // if the interval is not shrinking,
42+
// its size already equals O(1)
43+
changed = false;
44+
mid1 = left + (right - left) / 3;
45+
mid2 = right - (right - left) / 3;
46+
if ((pattern and values[mid1] < values[mid2])
47+
or (!pattern and values[mid1] > values[mid2])) {
48+
changed |= (right != mid2);
49+
right = mid2;
50+
}
51+
else {
52+
changed |= (left != mid1);
53+
left = mid1;
54+
}
55+
}
56+
57+
T min_value = values[left];
58+
T max_value = values[left];
59+
size_t min_index = left;
60+
size_t max_index = left;
61+
for (size_t index = left + 1; index <= right; index++) {
62+
if (min_value > values[index]) {
63+
min_value = values[index];
64+
min_index = index;
65+
}
66+
if (max_value < values[index]) {
67+
max_value = values[index];
68+
max_index = index;
69+
}
70+
}
71+
72+
return pattern == ASCEND_THEN_DESCEND ? max_index : min_index;
73+
}
74+
75+
#ifndef TERNARY_SEARCH_TEST
76+
int main() {
77+
int size;
78+
cout << "Enter the input size : ";
79+
cin >> size;
80+
81+
vector <int> input_values(size); //supposedly unimodal array
82+
cout << "Enter " << size << " values:\n";
83+
for (int& value: input_values)
84+
cin >> value;
85+
86+
string answer;
87+
cout << "\nDo the values first ascend then descend?"
88+
<< "If otherwise, enter \"no\".\n"
89+
<< "[Y]es / [n]o : ";
90+
getline(cin, answer);
91+
92+
Pattern pattern = ASCEND_THEN_DESCEND; // answer is "yes" by default
93+
if (answer[0] == 'n' or answer[0] == 'N') // user answers "no"
94+
pattern = Pattern::DESCEND_THEN_ASCEND;
95+
96+
int index = ternary_search(input_values, pattern);
97+
if (pattern == ASCEND_THEN_DESCEND)
98+
cout << "Maximum value has position " << index << "\n";
99+
else
100+
cout << "Minimum value has position " << index << "\n";
101+
102+
return 0;
103+
}
104+
#endif

0 commit comments

Comments
 (0)