|
10 | 10 | */ |
11 | 11 | function ternarySearchByRecursion($arr, $key, $low, $high) |
12 | 12 | { |
13 | | - |
14 | | - //Return -1 if high lesser than low, we can't find item in the whole array |
| 13 | + // Return null if high is less than low (base case: key not found). |
15 | 14 | if ($high < $low) { |
16 | 15 | return null; |
17 | 16 | } |
18 | 17 |
|
19 | | - //get $mid1 and $mid2 |
| 18 | + // Calculate the indices of the first and second midpoints. |
20 | 19 | $mid1 = floor($low + ($high - $low) / 3); |
21 | 20 | $mid2 = floor($high - ($high - $low) / 3); |
22 | | -// check if $key is found at any $mid |
| 21 | + |
| 22 | + // Check if key is located at either midpoint. |
23 | 23 | if ($arr[$mid1] === $key) { |
24 | | -// return index of $key if found |
25 | 24 | return $mid1; |
26 | 25 | } |
| 26 | + |
27 | 27 | if ($arr[$mid2] === $key) { |
28 | | -// return index of $key if found |
29 | 28 | return $mid2; |
30 | 29 | } |
31 | 30 |
|
32 | | - // since the $key is not found at $mid, |
33 | | - // check in which region it is present |
34 | | - // and repeat the Search operation |
35 | | - // in that region |
| 31 | + // Determine which section to continue searching in. |
36 | 32 | if ($key < $arr[$mid1]) { |
37 | | -// the $key lies in between $low and $mid1 |
| 33 | + // Key is in the left section, between $low and $mid1. |
38 | 34 | return ternarySearchByRecursion($arr, $key, $low, $mid1 - 1); |
39 | 35 | } elseif ($key > $arr[$mid2]) { |
40 | | - // the $key lies in between $mid2 and $high |
| 36 | + // Key is in the right section, between $mid2 and $high. |
41 | 37 | return ternarySearchByRecursion($arr, $key, $mid2 + 1, $high); |
42 | 38 | } else { |
43 | | - // the $key lies in between $mid1 and $mid2 |
| 39 | + // Key is in the middle section, between $mid1 and $mid2. |
44 | 40 | return ternarySearchByRecursion($arr, $key, $mid1 + 1, $mid2 - 1); |
45 | 41 | } |
46 | 42 | } |
47 | 43 |
|
48 | 44 | function ternarySearchIterative($arr, $key) |
49 | 45 | { |
| 46 | + // Initialize low and high pointers. |
50 | 47 | $low = 0; |
51 | 48 | $high = count($arr) - 1; |
| 49 | + |
| 50 | + // Continue searching while the high pointer is greater than or equal to the low pointer. |
52 | 51 | while ($high >= $low) { |
53 | | - // find the $mid1 and $mid2 |
| 52 | + // Calculate the first and second midpoints. |
54 | 53 | $mid1 = floor($low + ($high - $low) / 3); |
55 | 54 | $mid2 = floor($high - ($high - $low) / 3); |
56 | | - // check if $key is found at any $mid |
| 55 | + |
| 56 | + // Check if the key is found at either midpoint. |
57 | 57 | if ($arr[$mid1] === $key) { |
58 | | -// return index of $key if found |
59 | 58 | return $mid1; |
60 | 59 | } |
| 60 | + |
61 | 61 | if ($arr[$mid2] === $key) { |
62 | | - // return index of $key if found |
63 | 62 | return $mid2; |
64 | 63 | } |
65 | 64 |
|
66 | | - // since the $key is not found at $mid, |
67 | | - // check in which region it is present |
68 | | - // and repeat the Search operation |
69 | | - // in that region |
| 65 | + // Determine the section to continue the search in. |
70 | 66 | if ($key < $arr[$mid1]) { |
71 | | - // the $key lies in between $low and $mid1 |
| 67 | + // Key is in the left section, update the high pointer. |
72 | 68 | $high = $mid1 - 1; |
73 | 69 | } elseif ($key > $arr[$mid2]) { |
74 | | - // the $key lies in between $mid2 and $high |
| 70 | + // Key is in the right section, update the low pointer. |
75 | 71 | $low = $mid2 + 1; |
76 | 72 | } else { |
77 | | - // the $key lies in between $mid1 and $mid2 |
| 73 | + // Key is in the middle section, update both pointers. |
78 | 74 | $low = $mid1 + 1; |
79 | 75 | $high = $mid2 - 1; |
80 | 76 | } |
81 | 77 | } |
82 | | - // the $key was not found |
| 78 | + |
| 79 | + // Key was not found. |
83 | 80 | return null; |
84 | 81 | } |
0 commit comments