diff --git a/src/_Searching_/TernarySearch/TernarySearch.test.js b/src/_Searching_/TernarySearch/TernarySearch.test.js new file mode 100644 index 00000000..cc72b71c --- /dev/null +++ b/src/_Searching_/TernarySearch/TernarySearch.test.js @@ -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, 8)).toEqual(7); + }); + it('Ternary serach with recursion', () => { + expect(ternarySearchRecursive(array, low, high, 8)).toEqual(7); + }); + }); + 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, 4)).toEqual(3); + }); + }); + 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); + }); + }); + +}); diff --git a/src/_Searching_/TernarySearch/index.js b/src/_Searching_/TernarySearch/index.js new file mode 100644 index 00000000..81a33ff1 --- /dev/null +++ b/src/_Searching_/TernarySearch/index.js @@ -0,0 +1,53 @@ +/** + * Note: Array must be sorted for ternary 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 low = 0; + let high = arr.length - 1; + while(low <= high){ + let lowMiddle = low + Math.floor((high - low) / 3); + let highMiddle = low + 2 * Math.floor((high - low) / 3); + if(key == arr[low]){ + return low; + } else if(key == arr[high]){ + return high; + } else if(key <= arr[lowMiddle]){ + high = lowMiddle; + } else if(key > arr[lowMiddle] && key <= arr[highMiddle]){ + low = lowMiddle + 1; + high = highMiddle; + } else { + low = highMiddle + 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[lowMiddle]){ + return ternarySearchRecursive(arr, low, lowMiddle - 1, key); + } else if(key > arr[lowMiddle] && key < arr[highMiddle]){ + return ternarySearchRecursive(arr, lowMiddle + 1, highMiddle - 1, key); + } else { + return ternarySearchRecursive(arr, highMiddle + 1, high, key); + } + } + return null; +} + +module.exports = { + ternarySearch, + ternarySearchRecursive +};