mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
Merge pull request #93 from ayDavidGitHere/master
Add ExponentialSearch Algorithm
This commit is contained in:
commit
d2b5406fd9
@ -42,10 +42,12 @@
|
||||
|
||||
## Searches
|
||||
* [Binarysearch](./Searches/BinarySearch.php)
|
||||
* [ExponentialSearch](./Searches/ExponentialSearch.php)
|
||||
* [Fibonaccisearch](./Searches/FibonacciSearch.php)
|
||||
* [Jumpsearch](./Searches/JumpSearch.php)
|
||||
* [Linearsearch](./Searches/LinearSearch.php)
|
||||
* [Lowerbound](./Searches/LowerBound.php)
|
||||
* [TernarySearch](./Searches/TernarySearch.php)
|
||||
* [Upperbound](./Searches/UpperBound.php)
|
||||
|
||||
## Sorting
|
||||
|
60
Searches/ExponentialSearch.php
Normal file
60
Searches/ExponentialSearch.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/*
|
||||
* Exponential Search Algorithm
|
||||
*
|
||||
* The algorithm consists of two stages.
|
||||
* The first stage determines a range in which the search key would reside
|
||||
**** if it were in the list.
|
||||
* In the second stage, a binary search is performed on this range.
|
||||
*/
|
||||
/**
|
||||
* @param Array $arr
|
||||
* @param int $value
|
||||
* @param int $floor
|
||||
* @param int $ceiling
|
||||
* @return int
|
||||
**/
|
||||
function binarySearch ($arr, $value, $floor, $ceiling) {
|
||||
// Get $middle index
|
||||
$mid = floor(($floor + $ceiling) / 2);
|
||||
// Return position if $value is at the $mid position
|
||||
if ($arr[$mid] === $value) {
|
||||
return (int) $mid;
|
||||
}
|
||||
//Return -1 is range is wrong
|
||||
if ($floor > $ceiling) return -1;
|
||||
// search the left part of the $array
|
||||
// If the $middle element is great than the $value
|
||||
if ($arr[$mid] > $value) {
|
||||
return binarySearch($arr, $value, $floor, $mid - 1);
|
||||
}
|
||||
// search the right part of the $array
|
||||
// If the $middle element is lower than the $value
|
||||
else {
|
||||
return binarySearch($arr, $value, $mid + 1, $ceiling);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param Array $arr
|
||||
* @param int $length
|
||||
* @param int $value
|
||||
* @return int
|
||||
**/
|
||||
function exponentialSearch ($arr, $value) {
|
||||
// If $value is the first element of the $array return this position
|
||||
if ($arr[0] === $value) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Find range for binary search
|
||||
$i = 1;
|
||||
$length = count($arr);
|
||||
while ($i < $length && $arr[$i] <= $value) {
|
||||
$i = $i * 2;
|
||||
}
|
||||
$floor = $i/2;
|
||||
$ceiling = min($i, $length);
|
||||
|
||||
// Call binary search for the range found above
|
||||
return binarySearch($arr, $value, $floor, $ceiling);
|
||||
}
|
83
Searches/TernarySearch.php
Normal file
83
Searches/TernarySearch.php
Normal file
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/* Ternary search is similar to binary search
|
||||
* It rather divides the sorted array into three parts rather than.two parts by using two middle points, $mid1 $mid2.
|
||||
* The value of the $key will first be compared with the two $mid points, the value will be returned if there is a match.
|
||||
* Then, if the value of the $key is less than $mid1, narrow the interval to the first part.
|
||||
* Else, if the value of the $key is greater than $mid2, narrow the interval to the third part.
|
||||
* Otherwise, narrow the interval to the $middle part.
|
||||
* Repeat the steps until the value is found or the interval is empty (value not found after checking all elements).
|
||||
*/
|
||||
|
||||
function ternarySearchByRecursion ($arr, $key, $low, $high) {
|
||||
|
||||
//Return -1 if high lesser than low, we can't find item in the whole array
|
||||
if ($high < $low) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
//get $mid1 and $mid2
|
||||
$mid1 = floor($low + ($high - $low) / 3);
|
||||
$mid2 = floor($high - ($high - $low) / 3);
|
||||
// check if $key is found at any $mid
|
||||
if ($arr[$mid1] === $key) {
|
||||
// return index of $key if found
|
||||
return $mid1;
|
||||
}
|
||||
if ($arr[$mid2] === $key) {
|
||||
// return index of $key if found
|
||||
return $mid2;
|
||||
}
|
||||
|
||||
// since the $key is not found at $mid,
|
||||
// check in which region it is present
|
||||
// and repeat the Search operation
|
||||
// in that region
|
||||
if ($key < $arr[$mid1]) {
|
||||
// the $key lies in between $low and $mid1
|
||||
return ternarySearchByRecursion($arr, $key, $low, $mid1 - 1);
|
||||
} else if ($key > $arr[$mid2]) {
|
||||
// the $key lies in between $mid2 and $high
|
||||
return ternarySearchByRecursion($arr, $key, $mid2 + 1, $high);
|
||||
} else {
|
||||
// the $key lies in between $mid1 and $mid2
|
||||
return ternarySearchByRecursion($arr, $key, $mid1 + 1, $mid2 - 1);
|
||||
}
|
||||
}
|
||||
|
||||
function ternarySearchIterative ($arr, $key) {
|
||||
$low = 0; $high = count($arr) - 1;
|
||||
while ($high >= $low) {
|
||||
// find the $mid1 and $mid2
|
||||
$mid1 = floor($low + ($high - $low) / 3);
|
||||
$mid2 = floor($high - ($high - $low) / 3);
|
||||
|
||||
// check if $key is found at any $mid
|
||||
if ($arr[$mid1] === $key) {
|
||||
// return index of $key if found
|
||||
return $mid1;
|
||||
}
|
||||
if ($arr[$mid2] === $key) {
|
||||
// return index of $key if found
|
||||
return $mid2;
|
||||
}
|
||||
|
||||
// since the $key is not found at $mid,
|
||||
// check in which region it is present
|
||||
// and repeat the Search operation
|
||||
// in that region
|
||||
if ($key < $arr[$mid1]) {
|
||||
// the $key lies in between $low and $mid1
|
||||
$high = $mid1 - 1;
|
||||
} else if ($key > $arr[$mid2]) {
|
||||
// the $key lies in between $mid2 and $high
|
||||
$low = $mid2 + 1;
|
||||
} else {
|
||||
// the $key lies in between $mid1 and $mid2
|
||||
$low = $mid1 + 1;
|
||||
$high = $mid2 - 1;
|
||||
}
|
||||
}
|
||||
// the $key was not found
|
||||
return null;
|
||||
}
|
@ -12,6 +12,8 @@ require_once __DIR__ . '/../../Searches/LinearSearch.php';
|
||||
require_once __DIR__ . '/../../Searches/LowerBound.php';
|
||||
require_once __DIR__ . '/../../Searches/UpperBound.php';
|
||||
require_once __DIR__ . '/../../Searches/JumpSearch.php';
|
||||
require_once __DIR__ . '/../../Searches/ExponentialSearch.php';
|
||||
require_once __DIR__ . '/../../Searches/TernarySearch.php';
|
||||
|
||||
|
||||
class SearchesTest extends TestCase
|
||||
@ -143,5 +145,46 @@ class SearchesTest extends TestCase
|
||||
$result = jumpSearch($list, $target);
|
||||
assertEquals(6, $result);
|
||||
}
|
||||
|
||||
|
||||
public function testExponentialSearch()
|
||||
{
|
||||
$list = array(2,3,4,7,28,35,63,98);
|
||||
$target = 35;
|
||||
$result = exponentialSearch($list, $target);
|
||||
assertEquals(5, $result);
|
||||
}
|
||||
|
||||
public function testTernarySearchIterative()
|
||||
{
|
||||
$list = [0, 5, 7, 10, 15];
|
||||
$target = 0;
|
||||
$result = ternarySearchIterative($list, $target);
|
||||
assertEquals(0, $result);
|
||||
$target = 15;
|
||||
$result = ternarySearchIterative($list, $target);
|
||||
assertEquals(4, $result);
|
||||
$target = 5;
|
||||
$result = ternarySearchIterative($list, $target);
|
||||
assertEquals(1, $result);
|
||||
$target = 6;
|
||||
$result = ternarySearchIterative($list, $target);
|
||||
assertEquals(null, $result);
|
||||
}
|
||||
|
||||
public function testTernarySearchByRecursion()
|
||||
{
|
||||
$list = [0, 5, 7, 10, 15];
|
||||
$target = 0;
|
||||
$result = ternarySearchByRecursion($list, $target, 0, 4);
|
||||
assertEquals(0, $result);
|
||||
$target = 15;
|
||||
$result = ternarySearchByRecursion($list, $target, 0, 4);
|
||||
assertEquals(4, $result);
|
||||
$target = 5;
|
||||
$result = ternarySearchByRecursion($list, $target, 0, 4);
|
||||
assertEquals(1, $result);
|
||||
$target = 6;
|
||||
$result = ternarySearchByRecursion($list, $target, 0, 4);
|
||||
assertEquals(null, $result);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user