|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Ternary search algorithm More...
#include <iostream>Macros | |
| #define | absolutePrecision 10 |
| #define | _target 10 |
| #define | MAX 10000000 |
| Maximum length of array. | |
Functions | |
| void | get_input () |
| int | it_ternary_search (int left, int right, int A[], int target) |
| int | rec_ternary_search (int left, int right, int A[], int target) |
| void | ternary_search (int N, int A[], int target) |
| int | main () |
Ternary search algorithm
This is a divide and conquer algorithm. It does this by dividing the search space by 3 parts and using its property (usually monotonic property) to find the desired index.
| #define _target 10 |
The value of _target should be decided or can be decided later by using the variable of the function.
| #define absolutePrecision 10 |
The absolutePrecision can be modified to fit preference but it is recommended to not go lower than 10 due to errors that may occur.
| void get_input | ( | ) |
get_input function is to receive input from standard IO
| int it_ternary_search | ( | int | left, |
| int | right, | ||
| int | A[], | ||
| int | target | ||
| ) |
This is the iterative method of the ternary search which returns the index of the element.
| [in] | left | lower interval limit |
| [in] | right | upper interval limit |
| [in] | A | array to search in |
| [in] | target | value to search for |
| int main | ( | void | ) |
Main function
| int rec_ternary_search | ( | int | left, |
| int | right, | ||
| int | A[], | ||
| int | target | ||
| ) |
This is the recursive method of the ternary search which returns the index of the element.
| [in] | left | lower interval limit |
| [in] | right | upper interval limit |
| [in] | A | array to search in |
| [in] | target | value to search for |
| void ternary_search | ( | int | N, |
| int | A[], | ||
| int | target | ||
| ) |
ternary_search is a template function You could either use it_ternary_search or rec_ternary_search according to preference.
| [in] | N | length of array |
| [in] | A | array to search in |
| [in] | target | value to search for |