Skip to content
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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

add Ternary Search code[C++] #541

wants to merge 11 commits into from

Conversation

Yak0xff
Copy link

@Yak0xff Yak0xff commented Oct 15, 2017

Fixes #481

By submitting this pull request I confirm I've read and complied with the below declarations.

  • I have read the Contribution guidelines and I am confident that my PR reflects them.
  • I have followed the coding guidelines for this project.
  • My code follows the skeleton code structure.
  • This pull request has a descriptive title. For example, Added {Algorithm/DS name} [{Language}], not Update README.md or Added new code.
  • This pull request will be closed if I fail to update it even once in a continuous time span of 7 days.

@singhpratyush
Copy link
Member

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!

Copy link
Member

@mohitkyadav mohitkyadav left a 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.

* Time Complexity : O(max(input))
*/
int ternary_search (int ar[],int n, int left, int right, int x)
{
Copy link
Member

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) {
    }
}


int main()
{
int s = 12;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent correctly.

}
else
{
cout<<"The index is:"<<ternary_search(v,s,left-1,right-1,x)<<"\n";
Copy link
Member

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.

{
int s = 12;
int v[s];
short x;
Copy link
Member

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.

return right;
}

// Update the two index left and right if the element is not found.
Copy link
Member

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.

ar[i - 1] = i;
}
cout << "Enter number for research:\n";
cin >> x;
Copy link
Member

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.

int left = space / 3;
int right = (space / 3) * 2;

if(ternary_search(ar,space,left - 1,right - 1,x) == -1){
Copy link
Member

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).

* x : element to be searched.
* Time Complexity : O(max(input))
*/
int ternary_search (int ar[],int n, int left, int right, int x){
Copy link
Member

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).



if(x < ar[left]){
return ternary_search(ar,n,left - 1,right,x);
Copy link
Member

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).

}

if (x > ar[left] && x < ar[right]){
return ternary_search(ar,n,left + 1,right - 1,x);
Copy link
Member

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).

}

if(x > ar[right]){
return ternary_search(ar,n,left,right + 1,x);
Copy link
Member

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).

Copy link
Member

@mohitkyadav mohitkyadav left a 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.

@singhpratyush
Copy link
Member

@RobinChao: Are you still on it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ternary Search [C++]
4 participants