-
Notifications
You must be signed in to change notification settings - Fork 270
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
Conversation
@Kyle-Ski, we are not getting your commits please setup git in your system properly |
@ashokdey I've fixed the issue with the commits, please let me know if you are able to see them correctly |
} else if(arr.indexOf(key) == - 1){ | ||
return null; |
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.
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]){ |
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.
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); |
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.
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); |
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.
return ternarySearchRecursive(arr, lowMiddle+1, highMiddle-1, key);
} else if(arr.indexOf(key) == - 1){ | ||
return null; | ||
} else { | ||
return ternarySearchRecursive(arr, highMiddle, high, key); |
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.
return ternarySearchRecursive(arr, highMiddle+1, high, key);
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.
Hi @Kyle-Ski can you change the implementation of function ternarySearch
? It's confusing.
Implement it like function ternarySearchRecursive
…ests now correct
@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){ |
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.
high >= low
} else if(key === arr[high]){ | ||
return high; |
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.
Can you remove this extra IF condition?
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.
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
@TheSTL Thanks for the help fixing the recursive function! I also commented like you suggested. |
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.
Thanks @Kyle-Ski for the PR. 💯
Attempt to solve issue #72