Skip to content

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

Diff for: src/_Searching_/TernarySearch/TernarySearch.test.js

+1-1
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 ', () => {

Diff for: src/_Searching_/TernarySearch/index.js

+1-3
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)