12
12
*/
13
13
14
14
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
19
17
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 )
29
21
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 )
44
39
} 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 )
47
42
}
48
43
}
49
44
@@ -54,14 +49,11 @@ function ternarySearchIterative(arr, key, low = 0, high = arr.length - 1) {
54
49
const mid2 = Math . floor ( high - ( high - low ) / 3 )
55
50
56
51
// 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
65
57
66
58
// since the key is not found at mid,
67
59
// check in which region it is present
@@ -75,8 +67,7 @@ function ternarySearchIterative(arr, key, low = 0, high = arr.length - 1) {
75
67
low = mid2 + 1
76
68
} else {
77
69
// the key lies in between mid1 and mid2
78
- low = mid1 + 1
79
- high = mid2 - 1
70
+ ; [ low , high ] = [ mid1 + 1 , mid2 - 1 ]
80
71
}
81
72
}
82
73
// the key was not found
0 commit comments