Skip to content

Commit 163f1c0

Browse files
committed
chore: improve Ternary Search algorithm
1 parent b078444 commit 163f1c0

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

Search/TernarySearch.js

+30-39
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,33 @@
1212
*/
1313

1414
function ternarySearchRecursive(arr, key, low = 0, high = arr.length - 1) {
15-
if (high >= low) {
16-
// find the mid1 and mid2
17-
const mid1 = Math.floor(low + (high - low) / 3)
18-
const mid2 = Math.floor(high - (high - low) / 3)
15+
// if low > high => we have searched the whole array without finding the item
16+
if (low > high) return -1
1917

20-
// check if key is found at any mid
21-
if (arr[mid1] === key) {
22-
// return index of key if found
23-
return mid1
24-
}
25-
if (arr[mid2] === key) {
26-
// return index of key if found
27-
return mid2
28-
}
18+
// find the mid1 and mid2
19+
const mid1 = Math.floor(low + (high - low) / 3)
20+
const mid2 = Math.floor(high - (high - low) / 3)
2921

30-
// since the key is not found at mid,
31-
// check in which region it is present
32-
// and repeat the Search operation
33-
// in that region
34-
if (key < arr[mid1]) {
35-
// the key lies in between low and mid1
36-
return ternarySearchRecursive(arr, key, low, mid1 - 1)
37-
} else if (key > arr[mid2]) {
38-
// the key lies in between mid2 and high
39-
return ternarySearchRecursive(arr, key, mid2 + 1, high)
40-
} else {
41-
// the key lies in between mid1 and mid2
42-
return ternarySearchRecursive(arr, key, mid1 + 1, mid2 - 1)
43-
}
22+
// check if key is found at any mid
23+
// return index of key if found
24+
if (arr[mid1] === key) return mid1
25+
26+
// return index of key if found
27+
if (arr[mid2] === key) return mid2
28+
29+
// since the key is not found at mid,
30+
// check in which region it is present
31+
// and repeat the Search operation
32+
// in that region
33+
if (key < arr[mid1]) {
34+
// the key lies in between low and mid1
35+
return ternarySearchRecursive(arr, key, low, mid1 - 1)
36+
} else if (key > arr[mid2]) {
37+
// the key lies in between mid2 and high
38+
return ternarySearchRecursive(arr, key, mid2 + 1, high)
4439
} else {
45-
// if low > high => we have searched the whole array without finding the item
46-
return -1
40+
// the key lies in between mid1 and mid2
41+
return ternarySearchRecursive(arr, key, mid1 + 1, mid2 - 1)
4742
}
4843
}
4944

@@ -54,14 +49,11 @@ function ternarySearchIterative(arr, key, low = 0, high = arr.length - 1) {
5449
const mid2 = Math.floor(high - (high - low) / 3)
5550

5651
// check if key is found at any mid
57-
if (arr[mid1] === key) {
58-
// return index of key if found
59-
return mid1
60-
}
61-
if (arr[mid2] === key) {
62-
// return index of key if found
63-
return mid2
64-
}
52+
// return index of key if found
53+
if (arr[mid1] === key) return mid1
54+
55+
// return index of key if found
56+
if (arr[mid2] === key) return mid2
6557

6658
// since the key is not found at mid,
6759
// check in which region it is present
@@ -75,8 +67,7 @@ function ternarySearchIterative(arr, key, low = 0, high = arr.length - 1) {
7567
low = mid2 + 1
7668
} else {
7769
// the key lies in between mid1 and mid2
78-
low = mid1 + 1
79-
high = mid2 - 1
70+
;[low, high] = [mid1 + 1, mid2 - 1]
8071
}
8172
}
8273
// the key was not found

Search/test/TernarySearch.test.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ test('should return the index of a number in an array of numbers:', () => {
2929
test('should return the index of a string in an array of strings:', () => {
3030
const indexNumber = ternarySearchRecursive(
3131
['Ali', 'Cathrynli', 'Josuke', 'Thomas'],
32-
'Cathrynli'
32+
'Josuke'
3333
)
34-
expect(indexNumber).toBe(1)
34+
expect(indexNumber).toBe(2)
3535
})
3636

3737
test('should return the index of a string in an array of strings:', () => {
38-
const indexNumber = ternarySearchRecursive(
38+
const indexNumber = ternarySearchIterative(
3939
['Ali', 'Cathrynli', 'Josuke', 'Thomas'],
4040
'Josuke'
4141
)
@@ -49,3 +49,11 @@ test('should return the index of a string in an array of strings:', () => {
4949
)
5050
expect(indexNumber).toBe(-1)
5151
})
52+
53+
test('should return the index of a string in an array of strings:', () => {
54+
const indexNumber = ternarySearchIterative(
55+
['Ali', 'Cathrynli', 'Josuke', 'Thomas'],
56+
'Angela'
57+
)
58+
expect(indexNumber).toBe(-1)
59+
})

0 commit comments

Comments
 (0)