Skip to content

Commit

Permalink
Create SentinelSearch.php (TheAlgorithms#124)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Abhishek-Pashte and darwinz authored Jun 14, 2024
1 parent 5d9350b commit 8389d29
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
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

0 comments on commit 8389d29

Please sign in to comment.