mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-07-12 20:46:26 +02:00
update ExponentialSearch and added TernarySearch
Added ExponentialSearch and TernarySearch to Test/Searches and updated Directory.md
This commit is contained in:
@ -7,14 +7,6 @@
|
|||||||
**** if it were in the list.
|
**** if it were in the list.
|
||||||
* In the second stage, a binary search is performed on this range.
|
* In the second stage, a binary search is performed on this range.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Array $arr
|
* @param Array $arr
|
||||||
* @param int $value
|
* @param int $value
|
||||||
@ -25,18 +17,12 @@
|
|||||||
function binarySearch ($arr, $value, $floor, $ceiling) {
|
function binarySearch ($arr, $value, $floor, $ceiling) {
|
||||||
// Get $middle index
|
// Get $middle index
|
||||||
$mid = floor(($floor + $ceiling) / 2);
|
$mid = floor(($floor + $ceiling) / 2);
|
||||||
|
|
||||||
// Return position if $value is at the $mid position
|
// Return position if $value is at the $mid position
|
||||||
if ($arr[$mid] === $value) {
|
if ($arr[$mid] === $value) {
|
||||||
return (int) $mid;
|
return (int) $mid;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Return -1 is range is wrong
|
//Return -1 is range is wrong
|
||||||
if ($floor > $ceiling) return -1;
|
if ($floor > $ceiling) return -1;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// search the left part of the $array
|
// search the left part of the $array
|
||||||
// If the $middle element is great than the $value
|
// If the $middle element is great than the $value
|
||||||
if ($arr[$mid] > $value) {
|
if ($arr[$mid] > $value) {
|
||||||
@ -48,22 +34,13 @@ function binarySearch ($arr, $value, $floor, $ceiling) {
|
|||||||
return binarySearch($arr, $value, $mid + 1, $ceiling);
|
return binarySearch($arr, $value, $mid + 1, $ceiling);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Array $arr
|
* @param Array $arr
|
||||||
* @param int $length
|
* @param int $length
|
||||||
* @param int $value
|
* @param int $value
|
||||||
* @return int
|
* @return int
|
||||||
**/
|
**/
|
||||||
function exponentialSearch ($arr, $length, $value) {
|
function exponentialSearch ($arr, $value) {
|
||||||
// If $value is the first element of the $array return this position
|
// If $value is the first element of the $array return this position
|
||||||
if ($arr[0] === $value) {
|
if ($arr[0] === $value) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -71,6 +48,7 @@ function exponentialSearch ($arr, $length, $value) {
|
|||||||
|
|
||||||
// Find range for binary search
|
// Find range for binary search
|
||||||
$i = 1;
|
$i = 1;
|
||||||
|
$length = count($arr);
|
||||||
while ($i < $length && $arr[$i] <= $value) {
|
while ($i < $length && $arr[$i] <= $value) {
|
||||||
$i = $i * 2;
|
$i = $i * 2;
|
||||||
}
|
}
|
||||||
@ -80,20 +58,3 @@ function exponentialSearch ($arr, $length, $value) {
|
|||||||
// Call binary search for the range found above
|
// Call binary search for the range found above
|
||||||
return binarySearch($arr, $value, $floor, $ceiling);
|
return binarySearch($arr, $value, $floor, $ceiling);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function Test(){
|
|
||||||
$arr = [2, 3, 4, 7, 28, 35, 63, 98];
|
|
||||||
$value = 35;
|
|
||||||
$result = exponentialSearch($arr, count($arr), $value);
|
|
||||||
|
|
||||||
var_dump($result);
|
|
||||||
}
|
|
||||||
//Test();
|
|
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 -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//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 -1;
|
||||||
|
}
|
Reference in New Issue
Block a user