From 642f0533f7ebcbcae49928a5ad36edc6dd9981a2 Mon Sep 17 00:00:00 2001 From: Kartikey Tripathi Date: Tue, 2 Oct 2018 00:00:44 +0530 Subject: [PATCH 1/4] Add Binary Search in C++ --- searching/binary_search.cpp | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 searching/binary_search.cpp diff --git a/searching/binary_search.cpp b/searching/binary_search.cpp new file mode 100644 index 0000000..de33bd0 --- /dev/null +++ b/searching/binary_search.cpp @@ -0,0 +1,62 @@ +/* + Binary Search + ----------- + A searching algorithm that finds the position of a target value + within a sorted array. + + Time complexity + --------------- + O(log(N)), where N is the number of elements in the array. + + Space complexity + ---------------- + O(1). +*/ + +#include +#include + +using namespace std; + +int binarySearch(const int value, const vector& sortedVect, const int low, const int high) { + int mid = (low + high) / 2; + + if (value == sortedVect[mid]) + return mid; + else if (low <= high) { + if (value < sortedVect[mid]) { + // value must be between indices low and mid-1, if exists + return binarySearch(value, sortedVect, low, mid-1); + } + else if (value > sortedVect[mid]) { + // value must be between indices mid-1 and high, if exists + return binarySearch(value, sortedVect, mid+1, high); + } + } + + return -1; +} + +int main() { + int value; + cout << "Enter the value to search for : "; + cin >> value; + + int size; + cout << "Enter the input size : "; + cin >> size; + + vector inputVect(size); // supposedly sorted values + cout << "Enter " << size << " integers in ascending order :\n"; + for (int& val: inputVect) + cin >> val; + + int index = binarySearch(value, inputVect, 0, size-1); + cout << "\n"; + if (index != -1) + cout << "Found " << value << " at position " << (index + 1) << "\n"; + else + cout << "Either " << value << " is not present among the input values, or the values aren\'t sorted\n"; + + return 0; +} From 87dbd784fa498cb45a3fb1cf5905096e970e55bc Mon Sep 17 00:00:00 2001 From: Kartikey Tripathi Date: Tue, 2 Oct 2018 00:04:14 +0530 Subject: [PATCH 2/4] Add Ternary Search in C++ --- searching/ternary_search.cpp | 104 +++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 searching/ternary_search.cpp diff --git a/searching/ternary_search.cpp b/searching/ternary_search.cpp new file mode 100644 index 0000000..14b7057 --- /dev/null +++ b/searching/ternary_search.cpp @@ -0,0 +1,104 @@ +/* + Ternary search + -------------- + A searching algorithm that finds the position of a maximum(minimum) value + within an unimodal array. + + Time complexity + --------------- + O(log(N)), where N is the number of elements in the array. + + Space complexity + ---------------- + O(1). +*/ + +#include +#include + +using namespace std; + +enum Pattern { + ASCEND_THEN_DESCEND, + DESCEND_THEN_ASCEND +}; + +/* + ternary_search + ------------- + If the values first ascend and then descend, this function finds the position + of the maximum value. Otherwise, if they first descend and then ascend, it + finds the position of the minimum value. +*/ +template +size_t ternary_search(const vector& values, const Pattern& pattern) { + // left and right are the edges of the interval of search + size_t left = 0; + size_t right = values.size() - 1; + + bool changed = true; + size_t mid1, mid2; + while (right - left > 1 and changed) { // if the interval is not shrinking, + // its size already equals O(1) + changed = false; + mid1 = left + (right - left) / 3; + mid2 = right - (right - left) / 3; + if ((pattern and values[mid1] < values[mid2]) + or (!pattern and values[mid1] > values[mid2])) { + changed |= (right != mid2); + right = mid2; + } + else { + changed |= (left != mid1); + left = mid1; + } + } + + T min_value = values[left]; + T max_value = values[left]; + size_t min_index = left; + size_t max_index = left; + for (size_t index = left + 1; index <= right; index++) { + if (min_value > values[index]) { + min_value = values[index]; + min_index = index; + } + if (max_value < values[index]) { + max_value = values[index]; + max_index = index; + } + } + + return pattern == ASCEND_THEN_DESCEND ? max_index : min_index; +} + +#ifndef TERNARY_SEARCH_TEST +int main() { + int size; + cout << "Enter the input size : "; + cin >> size; + + vector input_values(size); //supposedly unimodal array + cout << "Enter " << size << " values:\n"; + for (int& value: input_values) + cin >> value; + + string answer; + cout << "\nDo the values first ascend then descend?" + << "If otherwise, enter \"no\".\n" + << "[Y]es / [n]o : "; + getline(cin, answer); + + Pattern pattern = ASCEND_THEN_DESCEND; // answer is "yes" by default + if (answer[0] == 'n' or answer[0] == 'N') // user answers "no" + pattern = Pattern::DESCEND_THEN_ASCEND; + + int index = ternary_search(input_values, pattern); + if (pattern == ASCEND_THEN_DESCEND) + cout << "Maximum value has position " << index << "\n"; + else + cout << "Minimum value has position " << index << "\n"; + + return 0; +} +#endif From 5261595a5b54053cd3b2aaa5fee603d1844e5617 Mon Sep 17 00:00:00 2001 From: Kartikey Tripathi Date: Tue, 2 Oct 2018 00:15:23 +0530 Subject: [PATCH 3/4] add name --- contributors.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contributors.md b/contributors.md index eb4db77..4c9d99f 100644 --- a/contributors.md +++ b/contributors.md @@ -8,4 +8,5 @@ 6. [Ujjval Patel](https://github.com/Ujjval-Patel) 7. [Shaguna Awasthi](https://github.com/Shagunaawasthi) 8. [Joy Banerjee](https://github.com/joybanerjee08) -9. [Your Name](https://github.com/yourprofile) +9. [Kartikey Tripathi](https://github.com/kartikeytripathi) +10. [Your Name](https://github.com/yourprofile) From 29f0437170d34deb2f5690d9d0ce9e8e87e5561f Mon Sep 17 00:00:00 2001 From: Kartikey Tripathi Date: Tue, 2 Oct 2018 00:20:59 +0530 Subject: [PATCH 4/4] Update contributors.md --- contributors.md | 1 - 1 file changed, 1 deletion(-) diff --git a/contributors.md b/contributors.md index 4c9d99f..660a954 100644 --- a/contributors.md +++ b/contributors.md @@ -9,4 +9,3 @@ 7. [Shaguna Awasthi](https://github.com/Shagunaawasthi) 8. [Joy Banerjee](https://github.com/joybanerjee08) 9. [Kartikey Tripathi](https://github.com/kartikeytripathi) -10. [Your Name](https://github.com/yourprofile)