mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 07:08:13 +01:00
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:
parent
5d9350b58f
commit
8389d29c76
@ -66,10 +66,12 @@
|
|||||||
* [Jumpsearch](./Searches/JumpSearch.php)
|
* [Jumpsearch](./Searches/JumpSearch.php)
|
||||||
* [Linearsearch](./Searches/LinearSearch.php)
|
* [Linearsearch](./Searches/LinearSearch.php)
|
||||||
* [Lowerbound](./Searches/LowerBound.php)
|
* [Lowerbound](./Searches/LowerBound.php)
|
||||||
|
* [SentinelSearch](./Searches/SentinelSearch.php)
|
||||||
* [Ternarysearch](./Searches/TernarySearch.php)
|
* [Ternarysearch](./Searches/TernarySearch.php)
|
||||||
* [Twopointers](./Searches/TwoPointers.php)
|
* [Twopointers](./Searches/TwoPointers.php)
|
||||||
* [Upperbound](./Searches/UpperBound.php)
|
* [Upperbound](./Searches/UpperBound.php)
|
||||||
|
|
||||||
|
|
||||||
## Sorting
|
## Sorting
|
||||||
* [Arraykeyssort](./Sorting/ArrayKeysSort.php)
|
* [Arraykeyssort](./Sorting/ArrayKeysSort.php)
|
||||||
* [Bubblesort](./Sorting/BubbleSort.php)
|
* [Bubblesort](./Sorting/BubbleSort.php)
|
||||||
|
42
Searches/SentinelSearch.php
Normal file
42
Searches/SentinelSearch.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,7 @@ require_once __DIR__ . '/../../Searches/JumpSearch.php';
|
|||||||
require_once __DIR__ . '/../../Searches/ExponentialSearch.php';
|
require_once __DIR__ . '/../../Searches/ExponentialSearch.php';
|
||||||
require_once __DIR__ . '/../../Searches/TernarySearch.php';
|
require_once __DIR__ . '/../../Searches/TernarySearch.php';
|
||||||
require_once __DIR__ . '/../../Searches/InterpolationSearch.php';
|
require_once __DIR__ . '/../../Searches/InterpolationSearch.php';
|
||||||
|
require_once __DIR__ . '/../../Searches/SentinelSearch.php';
|
||||||
require_once __DIR__ . '/../../Searches/TwoPointers.php';
|
require_once __DIR__ . '/../../Searches/TwoPointers.php';
|
||||||
|
|
||||||
|
|
||||||
@ -204,6 +205,23 @@ class SearchesTest extends TestCase
|
|||||||
$this->assertEquals(12, $result);
|
$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()
|
public function testTwoPointers()
|
||||||
{
|
{
|
||||||
$list = [1, 2, 4, 7, 8, 10, 11, 12, 15];
|
$list = [1, 2, 4, 7, 8, 10, 11, 12, 15];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user