Create JumpSearch.php

This commit is contained in:
1101 2022-08-03 10:01:55 +08:00 committed by GitHub
parent f555a8f58f
commit 905db107ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

50
Searches/JumpSearch.php Normal file
View File

@ -0,0 +1,50 @@
<?php
/**
* Jump Search algorithm in PHP
* References: https://www.geeksforgeeks.org/jump-search/
* The list must be sorted in ascending order before performing jumpSearch
*
* @param Array $list refers to a sorted list of integer
* @param integer $key refers to the integer target to be searched from the sorted list
* @return index of $key if found, otherwise -1 is returned
*/
function jumpSearch($list,$key)
{
/*number of elements in the sorted array*/
$num = count($list);
/*block size to be jumped*/
$step = (int)sqrt($num);
$prev = 0;
while ($list[min($step, $num)-1] < $key)
{
$prev = $step;
$step += (int)sqrt($num);
if ($prev >= $num)
return -1;
}
/*Performing linear search for $key in block*/
while ($list[$prev] < $key)
{
$prev++;
if ($prev == min($step, $num))
return -1;
}
return $list[$prev] === $key ? $prev : -1;
}
/*
*Test jumpSearch
*/
$list = array( 3,5,6,7,9,10,12,20,22,24 );
$key=12;
/*Print out index of $key in the sorted list*/
echo "The index of ".$key." is ".jumpSearch($list,$key);
return 0;
?>