Skip to content

Commit 8389d29

Browse files
Create SentinelSearch.php (#124)
* Create SentinelSearch.php Added Sentinel Search algorithm in Searches folder * Update SentinelSearch.php * Update SearchesTest.php Added tests for SentinelSearch.php * Update DIRECTORY.md Added link for sentinel search * Update SearchesTest.php Testcase corrected * Updated SentinelSearch.php * Updated SearchesTest.php * Update Searches/SentinelSearch.php * Update Searches/SentinelSearch.php * Update Searches/SentinelSearch.php * Update Searches/SentinelSearch.php * Update Searches/SentinelSearch.php * Update Searches/SentinelSearch.php * Update Searches/SentinelSearch.php --------- Co-authored-by: Brandon Johnson <[email protected]>
1 parent 5d9350b commit 8389d29

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@
6666
* [Jumpsearch](./Searches/JumpSearch.php)
6767
* [Linearsearch](./Searches/LinearSearch.php)
6868
* [Lowerbound](./Searches/LowerBound.php)
69+
* [SentinelSearch](./Searches/SentinelSearch.php)
6970
* [Ternarysearch](./Searches/TernarySearch.php)
7071
* [Twopointers](./Searches/TwoPointers.php)
7172
* [Upperbound](./Searches/UpperBound.php)
73+
7274

7375
## Sorting
7476
* [Arraykeyssort](./Sorting/ArrayKeysSort.php)

Searches/SentinelSearch.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/* SentinelSearch
4+
Input : -
5+
parameter 1: Array
6+
parameter 2: Target element
7+
8+
Output : -
9+
Returns index of element if found, else -1
10+
*/
11+
function SentinelSearch($list, $target)
12+
{
13+
//Length of array
14+
$len = sizeof($list);
15+
16+
//Store last element of array
17+
$lastElement = $list[$len - 1];
18+
19+
//Put target at the last position of array known as 'Sentinel'
20+
if ($lastElement == $target) {
21+
return ($len - 1);
22+
}
23+
//Put target at last index of array
24+
$list[$len - 1] = $target;
25+
26+
//Initialize variable to traverse through array
27+
$i = 0;
28+
29+
//Traverse through array to search target
30+
while ($list[$i] != $target) {
31+
$i++;
32+
}
33+
//Put last element at it's position
34+
$list[$len - 1] = $lastElement;
35+
36+
//If i in less than length, It means element is present in array
37+
if ($i < ($len - 1)) {
38+
return $i;
39+
} else {
40+
return -1;
41+
}
42+
}

tests/Searches/SearchesTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require_once __DIR__ . '/../../Searches/ExponentialSearch.php';
1313
require_once __DIR__ . '/../../Searches/TernarySearch.php';
1414
require_once __DIR__ . '/../../Searches/InterpolationSearch.php';
15+
require_once __DIR__ . '/../../Searches/SentinelSearch.php';
1516
require_once __DIR__ . '/../../Searches/TwoPointers.php';
1617

1718

@@ -204,6 +205,23 @@ public function testInterpolationSearch()
204205
$this->assertEquals(12, $result);
205206
}
206207

208+
public function testSentinelSearch()
209+
{
210+
$list = [1,3,5,2,4,13,18,23,25,30];
211+
$target = 1;
212+
$result = SentinelSearch($list, $target);
213+
$this->assertEquals(0, $result);
214+
$target = 2;
215+
$result = SentinelSearch($list, $target);
216+
$this->assertEquals(3, $result);
217+
$target = 1000;
218+
$result = SentinelSearch($list, $target);
219+
$this->assertEquals(-1, $result);
220+
$target = -2;
221+
$result = SentinelSearch($list, $target);
222+
$this->assertEquals(-1, $result);
223+
}
224+
207225
public function testTwoPointers()
208226
{
209227
$list = [1, 2, 4, 7, 8, 10, 11, 12, 15];

0 commit comments

Comments
 (0)