-
Notifications
You must be signed in to change notification settings - Fork 496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add Ternary Search code[C++] #541
base: master
Are you sure you want to change the base?
Conversation
Hi! Thanks for showing interest in contributing to this repository. Please make sure that you have ticked the points mentioned in PR description correctly. Thanks again! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a new entry to README and see comments.
ternary_search/ternary_search.cpp
Outdated
* Time Complexity : O(max(input)) | ||
*/ | ||
int ternary_search (int ar[],int n, int left, int right, int x) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow Google C++ Style Guide,
function(param) {
if(condition) {
}
}
ternary_search/ternary_search.cpp
Outdated
|
||
int main() | ||
{ | ||
int s = 12; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent correctly.
ternary_search/ternary_search.cpp
Outdated
} | ||
else | ||
{ | ||
cout<<"The index is:"<<ternary_search(v,s,left-1,right-1,x)<<"\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put space around <<
and all binary operators.
ternary_search/ternary_search.cpp
Outdated
{ | ||
int s = 12; | ||
int v[s]; | ||
short x; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use common english words instead of single letter variable names.
ternary_search/ternary_search.cpp
Outdated
return right; | ||
} | ||
|
||
// Update the two index left and right if the element is not found. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put comments above the relevant blocks and remove unnecessary blank lines.
ternary_search/ternary_search.cpp
Outdated
ar[i - 1] = i; | ||
} | ||
cout << "Enter number for research:\n"; | ||
cin >> x; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't ask for any input, take any sample value.
ternary_search/ternary_search.cpp
Outdated
int left = space / 3; | ||
int right = (space / 3) * 2; | ||
|
||
if(ternary_search(ar,space,left - 1,right - 1,x) == -1){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a space after each ,
(comma).
ternary_search/ternary_search.cpp
Outdated
* x : element to be searched. | ||
* Time Complexity : O(max(input)) | ||
*/ | ||
int ternary_search (int ar[],int n, int left, int right, int x){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a space after each ,
, (comma).
ternary_search/ternary_search.cpp
Outdated
|
||
|
||
if(x < ar[left]){ | ||
return ternary_search(ar,n,left - 1,right,x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a space after each ,
, (comma).
ternary_search/ternary_search.cpp
Outdated
} | ||
|
||
if (x > ar[left] && x < ar[right]){ | ||
return ternary_search(ar,n,left + 1,right - 1,x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a space after each ,
, (comma).
ternary_search/ternary_search.cpp
Outdated
} | ||
|
||
if(x > ar[right]){ | ||
return ternary_search(ar,n,left,right + 1,x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a space after each ,
, (comma).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use 4 spaces instead of tabs for indentation.
@RobinChao: Are you still on it? |
Fixes #481
By submitting this pull request I confirm I've read and complied with the below declarations.
Added {Algorithm/DS name} [{Language}]
, notUpdate README.md
orAdded new code
.