TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Ternary search algorithm More...
#include <iostream>
Go to the source code of this file.
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.
Definition in file ternary_search.cpp.
#define _target 10 |
The value of _target should be decided or can be decided later by using the variable of the function.
Definition at line 27 of file ternary_search.cpp.
#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.
Definition at line 22 of file ternary_search.cpp.
#define MAX 10000000 |
Maximum length of array.
Definition at line 29 of file ternary_search.cpp.
void get_input | ( | ) |
get_input function is to receive input from standard IO
Definition at line 36 of file ternary_search.cpp.
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 |
Definition at line 48 of file ternary_search.cpp.
int main | ( | void | ) |
Main function
Definition at line 134 of file ternary_search.cpp.
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 |
Definition at line 90 of file ternary_search.cpp.
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 |
Definition at line 127 of file ternary_search.cpp.