diff --git a/README.md b/README.md index b1770895..152aa24b 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ Community (college) maintained list of Algorithms and Data Structures implementa | [Rod Cutting Problem](http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/) | [:white_check_mark:](rod_cutting_problem/rod_cutting.c) | | [:white_check_mark:](rod_cutting_problem/RodCutting.java) | [:white_check_mark:](rod_cutting_problem/rod_cutting.py) | [:white_check_mark:](rod_cutting_problem/rod_cutting.go) | [:white_check_mark:](rod_cutting_problem/rodCuttingProblem.js) | | | [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | [:white_check_mark:](shell_sort/ShellSort.cpp) | [:white_check_mark:](shell_sort/ShellSort.java) | [:white_check_mark:](/shell_sort/shell_sort.py) | [:white_check_mark:](shell_sort/shell_sort.go) | [:white_check_mark:](shell_sort/shellSort.js) | [:white_check_mark:](shell_sort/ShellSort.cs) | | [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) | [:white_check_mark:](sieve_of_eratosthenes/sieveOfEratosthenes.c) | | [:white_check_mark:](sieve_of_eratosthenes/SieveOfEratosthenes.java) | [:white_check_mark:](sieve_of_eratosthenes/sieve_of_eratosthenes.py) | [:white_check_mark:](sieve_of_eratosthenes/sieve_of_eratosthenes.go) | [:white_check_mark:](sieve_of_eratosthenes/sieveOfEratosthenes.js) | | -| [Sleep Sort](http://www.geeksforgeeks.org/sleep-sort-king-laziness-sorting-sleeping/) | | [:white_check_mark:](sleep_sort/sleep_sort.cpp) | [:white_check_mark:](sleep_sort/SleepSort.java) | [:white_check_mark:](sleep_sort/sleep_sort.py) | [:white_check_mark:](sleep_sort/sleep_sort.go) | | | | +| [Sleep Sort](http://www.geeksforgeeks.org/sleep-sort-king-laziness-sorting-sleeping/) | | [:white_check_mark:](sleep_sort/sleep_sort.cpp) | [:white_check_mark:](sleep_sort/SleepSort.java) | [:white_check_mark:](sleep_sort/sleep_sort.py) | [:white_check_mark:](sleep_sort/sleep_sort.go) | +| [Ternary Search](https://en.wikipedia.org/wiki/Ternary_search) | | [:white_check_mark:](sleep_sort/sleep_sort.cpp) | | | | | ## Implemented Data Structures diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp new file mode 100644 index 00000000..f97f5a15 --- /dev/null +++ b/ternary_search/ternary_search.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; +/* + * Function implementing ternary search + * args: + * ar : Initial array containing elements (unsorted) + * n : the search tree's depth + * left : left index of array. + * right : right index of array. + * x : element to be searched. + * Time Complexity : O(max(input)) + */ +int ternary_search (int ar[], int n, int left, int right, int x){ + if(left < 0 || right > n - 1 || left > right){ + return -1; + } + if(x == ar[left]){ + return left; + } + if(x == ar[right]){ + return right; + } + // Update the two index left and right if the element is not found. + if(x < ar[left]){ + return ternary_search(ar, n, left - 1, right, x); + } + if (x > ar[left] && x < ar[right]){ + return ternary_search(ar, n, left + 1, right - 1, x); + } + if(x > ar[right]){ + return ternary_search(ar, n, left, right + 1, x); + } +} + + +int main(){ + int space = 12; + int ar[space]; + short x = 6; + for(int i = 1; i <= space; i++){ + ar[i - 1] = i; + } + int left = space / 3; + int right = (space / 3) * 2; + if(ternary_search(ar, space, left - 1, right - 1, x) == -1){ + cout << "Number does not exist in array.\n"; + }else{ + cout << "The index is:" << ternary_search(ar, space, left - 1, right - 1, x) << "\n"; + } + return 0; +} +