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 <bbj1979@gmail.com>
This commit is contained in:
Abhishek Pashte 2024-06-14 11:12:22 +05:30 committed by GitHub
parent 5d9350b58f
commit 8389d29c76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 62 additions and 0 deletions

View File

@ -66,10 +66,12 @@
* [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)
* [Bubblesort](./Sorting/BubbleSort.php)

View File

@ -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;
}
}

View File

@ -12,6 +12,7 @@ require_once __DIR__ . '/../../Searches/JumpSearch.php';
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';
@ -204,6 +205,23 @@ class SearchesTest extends TestCase
$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];