|
| 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