Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 760a68f

Browse files
committed
fixing recursive search, removing unnecessary if
1 parent 93f5bf3 commit 760a68f

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

‎src/_Searching_/TernarySearch/TernarySearch.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Ternary Search', () => {
2626
expect(ternarySearch(array, 3)).toEqual(2);
2727
});
2828
it('Ternary serach with recursion', () => {
29-
expect(ternarySearchRecursive(array, low, high, 3)).toEqual(2);
29+
expect(ternarySearchRecursive(array, low, high, 4)).toEqual(3);
3030
});
3131
});
3232
describe('When element to find is no present in array ', () => {

‎src/_Searching_/TernarySearch/index.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ function ternarySearch(arr, key){
2929
}
3030

3131
function ternarySearchRecursive(arr, low, high, key){
32-
if(high > low){
32+
if(high >= low){
3333
let highMiddle = low + 2 * Math.floor((high - low) / 3);
3434
let lowMiddle = low + Math.floor((high - low) / 3);
3535
if(key === arr[lowMiddle]){
3636
return lowMiddle;
3737
} else if(key === arr[highMiddle]){
3838
return highMiddle;
39-
} else if(key === arr[high]){
40-
return high;
4139
} else if(key < arr[lowMiddle]){
4240
return ternarySearchRecursive(arr, low, lowMiddle - 1, key);
4341
} else if(key > arr[lowMiddle] && key < arr[highMiddle]){

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /