-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { ternarySearch, ternarySearchRecursive } = require('.'); | ||
|
||
describe('Ternary Search', () => { | ||
const array = [1, 2, 3, 4, 5, 6, 7, 8]; | ||
const low = 0; | ||
const high = array.length - 1; | ||
|
||
describe('When element to find is at 1st position ', () => { | ||
it('Ternary search with Loop', () => { | ||
expect(ternarySearch(array, 1)).toEqual(0); | ||
}); | ||
it('Ternary serach with recursion', () => { | ||
expect(ternarySearchRecursive(array, low, high, 1)).toEqual(0); | ||
}); | ||
}); | ||
describe('When element to find is at last position ', () => { | ||
it('Ternary search with Loop', () => { | ||
expect(ternarySearch(array, 7)).toEqual(6); | ||
}); | ||
it('Ternary serach with recursion', () => { | ||
expect(ternarySearchRecursive(array, low, high, 7)).toEqual(6); | ||
}); | ||
}); | ||
describe('When element to find is at random position ', () => { | ||
it('Ternary search with Loop', () => { | ||
expect(ternarySearch(array, 3)).toEqual(2); | ||
}); | ||
it('Ternary serach with recursion', () => { | ||
expect(ternarySearchRecursive(array, low, high, 3)).toEqual(2); | ||
}); | ||
}); | ||
describe('When element to find is no present in array ', () => { | ||
it('Ternary search with Loop', () => { | ||
expect(ternarySearch(array, 10)).toEqual(null); | ||
}); | ||
it('Ternary serach with recursion', () => { | ||
expect(ternarySearchRecursive(array, low, high, 10)).toEqual(null); | ||
}); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Note: Array must be sorted for binary search | ||
* Complexity: | ||
* Worst case time complexity: O(log N) | ||
* Average case time complexity: O(log N) | ||
* Best case time complexity: O(1) | ||
* Space complexity: O(1) | ||
*/ | ||
function ternarySearch(arr, key){ | ||
let left = 0; | ||
let right = arr.length - 1; | ||
while(left <= right){ | ||
let temp2 = left + Math.floor((right - left) / 3); | ||
let temp3 = left + 2 * Math.floor((right - left) / 3); | ||
if(key == arr[left]){ | ||
return left; | ||
} else if(key == arr[right]){ | ||
return right; | ||
} else if(key < arr[left] || key > arr[right]){ | ||
return null; | ||
} else if(key <= arr[temp2]){ | ||
right = temp2; | ||
} else if(key > arr[temp2] && key <= arr[temp3]){ | ||
left = temp2 + 1; | ||
right = temp3; | ||
} else { | ||
left = temp3 + 1; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
function ternarySearchRecursive(arr, low, high, key){ | ||
|
||
if(high >= low){ | ||
let highMiddle = low + 2 * Math.floor((high - low) / 3); | ||
let lowMiddle = low + Math.floor((high - low) / 3); | ||
if(key === arr[lowMiddle]){ | ||
return 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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} 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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} else if(arr.indexOf(key) == - 1){ | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 . |
||
} else { | ||
return ternarySearchRecursive(arr, highMiddle, high, key); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
return null; | ||
} | ||
|
||
module.exports = { | ||
ternarySearch, | ||
ternarySearchRecursive | ||
}; |
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]){