Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create SentinelSearch.php #124

Merged
merged 15 commits into from
Jun 14, 2024
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@
* [Jumpsearch](./Searches/JumpSearch.php)
* [Linearsearch](./Searches/LinearSearch.php)
* [Lowerbound](./Searches/LowerBound.php)
* [SentinelSearch](./Searches/SentinelSearch.php)
* [Ternarysearch](./Searches/TernarySearch.php)
* [Twopointers](./Searches/TwoPointers.php)
* [Upperbound](./Searches/UpperBound.php)


## Sorting
* [Arraykeyssort](./Sorting/ArrayKeysSort.php)
Expand Down
42 changes: 42 additions & 0 deletions Searches/SentinelSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/* SentinelSearch
Input : -
parameter 1: Array
parameter 2: Target element

Output : -
Returns index of element if found, else -1
*/
function SentinelSearch($list, $target)
{
//Length of array
$len = sizeof($list);

//Store last element of array
$lastElement = $list[$len - 1];

//Put target at the last position of array known as 'Sentinel'
if ($lastElement == $target) {
return ($len - 1);
}
//Put target at last index of array
$list[$len - 1] = $target;

//Initialize variable to traverse through array
$i = 0;

//Traverse through array to search target
while ($list[$i] != $target) {
$i++;
}
//Put last element at it's position
$list[$len - 1] = $lastElement;

//If i in less than length, It means element is present in array
if ($i < ($len - 1)) {
return $i;
} else {
return -1;
}
}
18 changes: 18 additions & 0 deletions tests/Searches/SearchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require_once __DIR__ . '/../../Searches/ExponentialSearch.php';
require_once __DIR__ . '/../../Searches/TernarySearch.php';
require_once __DIR__ . '/../../Searches/InterpolationSearch.php';
require_once __DIR__ . '/../../Searches/SentinelSearch.php';
require_once __DIR__ . '/../../Searches/TwoPointers.php';


Expand Down Expand Up @@ -204,6 +205,23 @@ public function testInterpolationSearch()
$this->assertEquals(12, $result);
}

public function testSentinelSearch()
{
$list = [1,3,5,2,4,13,18,23,25,30];
$target = 1;
$result = SentinelSearch($list, $target);
$this->assertEquals(0, $result);
$target = 2;
$result = SentinelSearch($list, $target);
$this->assertEquals(3, $result);
$target = 1000;
$result = SentinelSearch($list, $target);
$this->assertEquals(-1, $result);
$target = -2;
$result = SentinelSearch($list, $target);
$this->assertEquals(-1, $result);
}

public function testTwoPointers()
{
$list = [1, 2, 4, 7, 8, 10, 11, 12, 15];
Expand Down
Loading