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 #76

Merged
merged 4 commits into from
Oct 16, 2019
Merged

Add Ternary search #76

merged 4 commits into from
Oct 16, 2019

Conversation

Kyle-Ski
Copy link
Contributor

Attempt to solve issue #72

@ashokdey
Copy link
Member

@Kyle-Ski, we are not getting your commits please setup git in your system properly

@Kyle-Ski
Copy link
Contributor Author

@ashokdey I've fixed the issue with the commits, please let me know if you are able to see them correctly

@TheSTL TheSTL mentioned this pull request Oct 12, 2019
Comment on lines 46 to 47
} else if(arr.indexOf(key) == - 1){
return null;
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this line of code because arr.indexOf take O(n) time .

return lowMiddle;
} else if(key === arr[highMiddle]){
return highMiddle;
} else if(key < arr[highMiddle]){
Copy link
Member

Choose a reason for hiding this comment

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

Code should be } else if(key < arr[lowMiddle]){

} else if(key === arr[highMiddle]){
return highMiddle;
} else if(key < arr[highMiddle]){
return ternarySearchRecursive(arr, low, lowMiddle, key);
Copy link
Member

Choose a reason for hiding this comment

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

return ternarySearchRecursive(arr, low, lowMiddle-1, key);

} else if(key < arr[highMiddle]){
return ternarySearchRecursive(arr, low, lowMiddle, key);
} else if(key > arr[lowMiddle] && key < arr[highMiddle]){
return ternarySearchRecursive(arr, lowMiddle, highMiddle, key);
Copy link
Member

Choose a reason for hiding this comment

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

return ternarySearchRecursive(arr, lowMiddle+1, highMiddle-1, key);

} else if(arr.indexOf(key) == - 1){
return null;
} else {
return ternarySearchRecursive(arr, highMiddle, high, key);
Copy link
Member

Choose a reason for hiding this comment

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

return ternarySearchRecursive(arr, highMiddle+1, high, key);

Copy link
Member

@TheSTL TheSTL left a comment

Choose a reason for hiding this comment

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

Hi @Kyle-Ski can you change the implementation of function ternarySearch? It's confusing.
Implement it like function ternarySearchRecursive

@Kyle-Ski
Copy link
Contributor Author

@TheSTL I've made the changes you requested, do the new variable names on the non-recursive function help?

}

function ternarySearchRecursive(arr, low, high, key){
if(high > low){
Copy link
Member

Choose a reason for hiding this comment

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

high >= low

Comment on lines 39 to 40
} else if(key === arr[high]){
return high;
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove this extra IF condition?

Copy link
Member

@TheSTL TheSTL left a comment

Choose a reason for hiding this comment

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

Test cases failed:
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const low = 0;
const high = array.length - 1;

ternarySearchRecursive(array, low, high, 4)

  • expected : 3
  • output : null

To fix this bug above changes are required. @Kyle-Ski

@ashokdey
Copy link
Member

ashokdey commented Oct 16, 2019

@Kyle-Ski do mark a comment on the issue you are solving (#72 in your case) so as to assign you the issues.

@Kyle-Ski
Copy link
Contributor Author

@TheSTL Thanks for the help fixing the recursive function! I also commented like you suggested.

Copy link
Member

@TheSTL TheSTL left a comment

Choose a reason for hiding this comment

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

Thanks @Kyle-Ski for the PR. 💯

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.

3 participants