From ed1a69311602fd444e3161a5154713a9fc191ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=B0=B8=E8=B6=85?= Date: Sun, 15 Oct 2017 20:50:17 +0800 Subject: [PATCH 01/10] add Ternary Search code[C++] --- ternary_search/ternary_search.cpp | 76 +++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 ternary_search/ternary_search.cpp diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp new file mode 100644 index 00000000..1d1e879e --- /dev/null +++ b/ternary_search/ternary_search.cpp @@ -0,0 +1,76 @@ +#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 s = 12; + int v[s]; + short x; + for(int i = 1; i <= s; i++) + { + v[i-1] = i; + } + cout << "Enter number for research:\n"; + cin >> x; + + int left = s/3; + int right = (s/3)*2; + + if(ternary_search(v,s,left-1,right-1,x) == -1) + { + cout<<"Number does not exist in array.\n"; + } + else + { + cout<<"The index is:"< Date: Mon, 16 Oct 2017 11:48:26 +0800 Subject: [PATCH 02/10] follow Google C++ Style --- ternary_search/ternary_search.cpp | 35 ++++++++++--------------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp index 1d1e879e..dea16592 100644 --- a/ternary_search/ternary_search.cpp +++ b/ternary_search/ternary_search.cpp @@ -11,51 +11,41 @@ using namespace std; * x : element to be searched. * Time Complexity : O(max(input)) */ -int ternary_search (int ar[],int n, int left, int right, int x) -{ +int ternary_search (int ar[],int n, int left, int right, int x){ - if(left < 0 || right > n-1 || left > right) - { + if(left < 0 || right > n-1 || left > right){ return -1; } - if(x == ar[left]) - { + if(x == ar[left]){ return left; } - if(x == ar[right]) - { + if(x == ar[right]){ return right; } // Update the two index left and right if the element is not found. - if(x < ar[left]) - { + if(x < ar[left]){ return ternary_search(ar,n,left-1,right,x); } - if (x > ar[left] && x < ar[right]) - { - + if (x > ar[left] && x < ar[right]){ return ternary_search(ar,n,left+1,right-1,x); } - if(x > ar[right]) - { + if(x > ar[right]){ return ternary_search(ar,n,left,right+1,x); } } -int main() -{ +int main(){ int s = 12; int v[s]; short x; - for(int i = 1; i <= s; i++) - { + for(int i = 1; i <= s; i++){ v[i-1] = i; } cout << "Enter number for research:\n"; @@ -64,12 +54,9 @@ int main() int left = s/3; int right = (s/3)*2; - if(ternary_search(v,s,left-1,right-1,x) == -1) - { + if(ternary_search(v,s,left-1,right-1,x) == -1){ cout<<"Number does not exist in array.\n"; - } - else - { + }else{ cout<<"The index is:"< Date: Mon, 16 Oct 2017 11:52:27 +0800 Subject: [PATCH 03/10] use common english words instead of single letter variable names. && put space around << --- ternary_search/ternary_search.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp index dea16592..75d6b7a6 100644 --- a/ternary_search/ternary_search.cpp +++ b/ternary_search/ternary_search.cpp @@ -16,6 +16,7 @@ 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; } @@ -42,22 +43,22 @@ int ternary_search (int ar[],int n, int left, int right, int x){ int main(){ - int s = 12; - int v[s]; + int space = 12; + int ar[space]; short x; - for(int i = 1; i <= s; i++){ - v[i-1] = i; + for(int i = 1; i <= space; i++){ + ar[i-1] = i; } cout << "Enter number for research:\n"; cin >> x; - int left = s/3; - int right = (s/3)*2; + int left = space/3; + int right = (space/3)*2; - if(ternary_search(v,s,left-1,right-1,x) == -1){ - cout<<"Number does not exist in array.\n"; + if(ternary_search(ar,space,left-1,right-1,x) == -1){ + cout << "Number does not exist in array.\n"; }else{ - cout<<"The index is:"< Date: Mon, 16 Oct 2017 11:54:40 +0800 Subject: [PATCH 04/10] put space around all binary operators. --- ternary_search/ternary_search.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp index 75d6b7a6..6f502582 100644 --- a/ternary_search/ternary_search.cpp +++ b/ternary_search/ternary_search.cpp @@ -29,15 +29,15 @@ int ternary_search (int ar[],int n, int left, int right, int x){ if(x < ar[left]){ - return ternary_search(ar,n,left-1,right,x); + 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); + return ternary_search(ar,n,left + 1,right - 1,x); } if(x > ar[right]){ - return ternary_search(ar,n,left,right+1,x); + return ternary_search(ar,n,left,right + 1,x); } } @@ -47,18 +47,18 @@ int main(){ int ar[space]; short x; for(int i = 1; i <= space; i++){ - ar[i-1] = i; + ar[i - 1] = i; } cout << "Enter number for research:\n"; cin >> x; - int left = space/3; - int right = (space/3)*2; + int left = space / 3; + int right = (space / 3) * 2; - if(ternary_search(ar,space,left-1,right-1,x) == -1){ + if(ternary_search(ar,space,left - 1,right - 1,x) == -1){ cout << "Number does not exist in array.\n"; }else{ - cout << "The index is:"< Date: Sun, 15 Oct 2017 22:56:28 -0500 Subject: [PATCH 05/10] Update ternary_search.cpp --- ternary_search/ternary_search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp index 6f502582..38fb0c02 100644 --- a/ternary_search/ternary_search.cpp +++ b/ternary_search/ternary_search.cpp @@ -13,7 +13,7 @@ using namespace std; */ int ternary_search (int ar[],int n, int left, int right, int x){ - if(left < 0 || right > n-1 || left > right){ + if(left < 0 || right > n - 1 || left > right){ return -1; } From 2dbfba3dc33c5bcca4db6e25ed867ada2db73819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=EF=A3=BF?= <1933549736@qq.com> Date: Sun, 15 Oct 2017 23:00:17 -0500 Subject: [PATCH 06/10] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fef45ff8..f999d88b 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 From a643653659db580d331d4a1afe5d0208ac8ae321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=EF=A3=BF?= <1933549736@qq.com> Date: Mon, 16 Oct 2017 06:14:56 -0500 Subject: [PATCH 07/10] update code style --- ternary_search/ternary_search.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp index 38fb0c02..ca7499ab 100644 --- a/ternary_search/ternary_search.cpp +++ b/ternary_search/ternary_search.cpp @@ -11,7 +11,7 @@ using namespace std; * x : element to be searched. * Time Complexity : O(max(input)) */ -int ternary_search (int ar[],int n, int left, int right, int x){ +int ternary_search (int ar[], int n, int left, int right, int x){ if(left < 0 || right > n - 1 || left > right){ return -1; @@ -26,18 +26,16 @@ int ternary_search (int ar[],int n, int left, int right, int x){ } // 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); + 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); + return ternary_search(ar, n, left + 1, right - 1, x); } if(x > ar[right]){ - return ternary_search(ar,n,left,right + 1,x); + return ternary_search(ar, n, left, right + 1, x); } } @@ -45,20 +43,19 @@ int ternary_search (int ar[],int n, int left, int right, int x){ int main(){ int space = 12; int ar[space]; - short x; + short x = 6; + for(int i = 1; i <= space; i++){ ar[i - 1] = i; } - cout << "Enter number for research:\n"; - cin >> x; int left = space / 3; int right = (space / 3) * 2; - if(ternary_search(ar,space,left - 1,right - 1,x) == -1){ + 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"; + cout << "The index is:" << ternary_search(ar, space, left - 1, right - 1, x) << "\n"; } return 0; } From fe3ecca1044017d0af13969c351ef91a56b609dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=EF=A3=BF?= <1933549736@qq.com> Date: Mon, 16 Oct 2017 06:15:28 -0500 Subject: [PATCH 08/10] Update ternary_search.cpp --- ternary_search/ternary_search.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp index ca7499ab..1887e367 100644 --- a/ternary_search/ternary_search.cpp +++ b/ternary_search/ternary_search.cpp @@ -12,7 +12,6 @@ using namespace std; * 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; } From 2268d256ab5c0923de3f0c694c72a5686c6b7a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=B0=B8=E8=B6=85?= Date: Mon, 16 Oct 2017 19:26:05 +0800 Subject: [PATCH 09/10] fix space err --- ternary_search/ternary_search.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp index 1887e367..6aeed7a6 100644 --- a/ternary_search/ternary_search.cpp +++ b/ternary_search/ternary_search.cpp @@ -4,13 +4,13 @@ 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. + * 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; @@ -19,20 +19,20 @@ int ternary_search (int ar[], int n, int left, int right, int x){ 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. + + // 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); } @@ -40,17 +40,17 @@ int ternary_search (int ar[], int n, int left, int right, int x){ int main(){ - int space = 12; + 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{ @@ -58,3 +58,4 @@ int main(){ } return 0; } + From 3f4148caa5aa306f12ebcdfb05a55a617307223e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=EF=A3=BF?= <1933549736@qq.com> Date: Mon, 16 Oct 2017 07:00:25 -0500 Subject: [PATCH 10/10] Update ternary_search.cpp --- ternary_search/ternary_search.cpp | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/ternary_search/ternary_search.cpp b/ternary_search/ternary_search.cpp index 6aeed7a6..f97f5a15 100644 --- a/ternary_search/ternary_search.cpp +++ b/ternary_search/ternary_search.cpp @@ -15,25 +15,20 @@ 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]){ + if(x == ar[left]){ return left; } - - if(x == ar[right]){ + if(x == ar[right]){ return right; } - - // Update the two index left and right if the element is not found. + // 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]){ + if (x > ar[left] && x < ar[right]){ return ternary_search(ar, n, left + 1, right - 1, x); } - - if(x > ar[right]){ + if(x > ar[right]){ return ternary_search(ar, n, left, right + 1, x); } } @@ -43,15 +38,12 @@ 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 left = space / 3; int right = (space / 3) * 2; - - if(ternary_search(ar, space, left - 1, right - 1, x) == -1){ + 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";