1. using PHP to describe order lookup and binary lookup (also called binary lookup) algorithm, sequential lookup must consider efficiency, the object can be an ordered array [go]
2. Sequential Lookup
<?php
$n is the number of array elements to find, $k the element to be found
function Seq_sch ($array, $n, $k)
{
$array [$n] = $k;
for ($i =0; $i < $n; $i + +)
{
if ($array [$i]== $k)
{
return true;
Break
}
}
if ($i < $n)//To determine whether to the end of the array
{
return $i;
}
Else
{
return false;
}
}
$array = Array (3, 6, 1, 9, 2, 10);
$n = count ($array);
$k = 8;
if (Seq_sch ($array, $n, $k))
{
echo "Sequential lookup success";
}
Else
{
echo "Sequential lookup failed";
}
?>
A sequential lookup is a specific location in a known unordered queue that finds the same number as a given keyword. The principle is to have the keyword be compared to the number in the queue from the first one, until you find the same number as the given keyword.
3. Two points Search
<?php
$low the minimum value in the array to be looked up, $high the largest value in the number, $k the keyword to find
function Bin_sch ($array, $low, $high, $k)
{
if ($low <= $high)
{
$mid = Intval (($low + $high)/2);
if ($array [$mid] = = $k)
{
return true;
}
ElseIf ($k < $array [$mid]
{
Return Bin_sch ($array, $low, $mid-1, $k);
}
Else
{
Return Bin_sch ($array, $mid +1, $high, $k);
}
}
return false;
}
$array = Array (1, 2, 4, 6, 8);
$low = min (1, 2, 4, 6, 8);
$high = Max (1, 2, 4, 6, 8);
$k = 8;
if (Bin_sch ($array, $low, $high, $k))
{
echo "Two-point search success";
}
else{
echo "Two-point lookup failed";
}
?>
"Two-point lookup Requirements": 1. Sequential storage structure must be used 2. Must be ordered by keyword size.
"Algorithmic process" first, compare the keywords in the middle position of the table with the Lookup keyword, if they are equal, find success; otherwise, use intermediate positional records to divide the table into the top and bottom two sub-tables, and if the middle position record has more keywords than the lookup keyword, look for the previous child table, or further find the latter one.
Repeat the process until you find a record that satisfies the criteria, make the lookup successful, or until the child table does not exist, the lookup is unsuccessful at this time.
Instance:
1, 2, 4, 6, 8
Suppose you are looking for a keyword of 2.
First find the number of the middle position in the numeric value, that is, 4, compared with the keyword 2, two number of different, then the array with the middle position keyword 4 as the dividing point into the front and back two word table, that is
{1 2} and {6 8}
The middle position keyword 4 is larger than the keyword 2 you are looking for, so check the previous child table {1 2}, which has 2 in the table, and finds success.
Sequential lookups and binary lookups