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 bc95e85

Browse files
Kyle-SkiTheSTL
authored andcommitted
Add Ternary search (knaxus#76)
* implamenting ternary search * adding complexity * changed `ternarySearch` to be more like recursive, fixed recursive, tests now correct * fixing recursive search, removing unnecessary if
1 parent b83abac commit bc95e85

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { ternarySearch, ternarySearchRecursive } = require('.');
2+
3+
describe('Ternary Search', () => {
4+
const array = [1, 2, 3, 4, 5, 6, 7, 8];
5+
const low = 0;
6+
const high = array.length - 1;
7+
8+
describe('When element to find is at 1st position ', () => {
9+
it('Ternary search with Loop', () => {
10+
expect(ternarySearch(array, 1)).toEqual(0);
11+
});
12+
it('Ternary serach with recursion', () => {
13+
expect(ternarySearchRecursive(array, low, high, 1)).toEqual(0);
14+
});
15+
});
16+
describe('When element to find is at last position ', () => {
17+
it('Ternary search with Loop', () => {
18+
expect(ternarySearch(array, 8)).toEqual(7);
19+
});
20+
it('Ternary serach with recursion', () => {
21+
expect(ternarySearchRecursive(array, low, high, 8)).toEqual(7);
22+
});
23+
});
24+
describe('When element to find is at random position ', () => {
25+
it('Ternary search with Loop', () => {
26+
expect(ternarySearch(array, 3)).toEqual(2);
27+
});
28+
it('Ternary serach with recursion', () => {
29+
expect(ternarySearchRecursive(array, low, high, 4)).toEqual(3);
30+
});
31+
});
32+
describe('When element to find is no present in array ', () => {
33+
it('Ternary search with Loop', () => {
34+
expect(ternarySearch(array, 10)).toEqual(null);
35+
});
36+
it('Ternary serach with recursion', () => {
37+
expect(ternarySearchRecursive(array, low, high, 10)).toEqual(null);
38+
});
39+
});
40+
41+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Note: Array must be sorted for ternary search
3+
* Complexity:
4+
* Worst case time complexity: O(log N)
5+
* Average case time complexity: O(log N)
6+
* Best case time complexity: O(1)
7+
* Space complexity: O(1)
8+
*/
9+
function ternarySearch(arr, key){
10+
let low = 0;
11+
let high = arr.length - 1;
12+
while(low <= high){
13+
let lowMiddle = low + Math.floor((high - low) / 3);
14+
let highMiddle = low + 2 * Math.floor((high - low) / 3);
15+
if(key == arr[low]){
16+
return low;
17+
} else if(key == arr[high]){
18+
return high;
19+
} else if(key <= arr[lowMiddle]){
20+
high = lowMiddle;
21+
} else if(key > arr[lowMiddle] && key <= arr[highMiddle]){
22+
low = lowMiddle + 1;
23+
high = highMiddle;
24+
} else {
25+
low = highMiddle + 1;
26+
}
27+
}
28+
return null;
29+
}
30+
31+
function ternarySearchRecursive(arr, low, high, key){
32+
if(high >= low){
33+
let highMiddle = low + 2 * Math.floor((high - low) / 3);
34+
let lowMiddle = low + Math.floor((high - low) / 3);
35+
if(key === arr[lowMiddle]){
36+
return lowMiddle;
37+
} else if(key === arr[highMiddle]){
38+
return highMiddle;
39+
} else if(key < arr[lowMiddle]){
40+
return ternarySearchRecursive(arr, low, lowMiddle - 1, key);
41+
} else if(key > arr[lowMiddle] && key < arr[highMiddle]){
42+
return ternarySearchRecursive(arr, lowMiddle + 1, highMiddle - 1, key);
43+
} else {
44+
return ternarySearchRecursive(arr, highMiddle + 1, high, key);
45+
}
46+
}
47+
return null;
48+
}
49+
50+
module.exports = {
51+
ternarySearch,
52+
ternarySearchRecursive
53+
};

0 commit comments

Comments
(0)

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