Skip to content
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

Merged
merged 4 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/_Searching_/TernarySearch/TernarySearch.test.js
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);
});
});

});
58 changes: 58 additions & 0 deletions src/_Searching_/TernarySearch/index.js
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]){
Copy link
Member

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]){

return ternarySearchRecursive(arr, low, lowMiddle, key);
Copy link
Member

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[lowMiddle] && key < arr[highMiddle]){
return ternarySearchRecursive(arr, lowMiddle, highMiddle, key);
Copy link
Member

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;
Copy link
Member

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 .

} else {
return ternarySearchRecursive(arr, highMiddle, high, key);
Copy link
Member

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);

}
}
return null;
}

module.exports = {
ternarySearch,
ternarySearchRecursive
};