First, bubble sort
Copy CodeThe code is as follows:
Bubble sort
function Bubble_sort ($array)
{
$count =count ($array);
if ($count <= 0) {
return false;
}
for ($i =0; $i < $count; $i + +) {
for ($j =0; $j < $count-$i-1; $j + +) {
if ($array [$j] > $array [$j +1]) {
$temp = $array [$j];
$array [$j]= $array [$j +1];
$array [$j +1]= $temp;
}
}
}
return $array;
}
Second, quick sort
Copy the Code code as follows:
Quick Line
function Quick_sort ($array)
{
$count =count ($array);
if ($count <= 1) {
return $array;
}
$key = $array [0];
$array _left=array ();
$array _right=array ();
for ($i =1; $i < $count; $i + +) {
if ($array [$i] < $key) {
$array _left[]= $array [$i];
}else{
$array _right[]= $array [$i];
}
}
$array _left=quick_sort ($array _left);
$array _right=quick_sort ($array _right);
Return Array_merge ($array _left,array ($key), $array _right);
}
$myarray =array (1,5,3,4,12,10,8);
Print_r (Bubble_sort ($myarray));
echo "
";
Print_r (Quick_sort ($myarray));
echo "
";
Third, the position of the first occurrence of the quick Find value
Copy the Code code as follows:
/**
* Quick Find where the value first appears
* @param array $array arrays
* @param string $k the value to find
* @param int $low The minimum key value of the lookup range
* @param int $high The maximum key value of the range
*/
function Search ($array, $k, $low =0, $high =0)
{
Determine if the call is the first time
if (count ($array)!=0 and $high = = 0) {
$high = count ($array);
}
If the remaining array elements are still present
if ($low <= $high) {
Take the middle value of $low and $high
$mid = Intval (($low + $high)/2);
If found then returns
if ($array [$mid] = = $k) {
return $mid;
}
If it is not found, continue to find
ElseIf ($k < $array [$mid]) {
Return Search ($array, $k, $low, $mid-1);
}
else {
Return Search ($array, $k, $mid +1, $high);
}
}
return-1;
}
$array = Array (4,5,7,8,9,10,8); Test the search function
Echo Search ($array, 8); Call the search function and output the lookup results
Four, remove the two-dimensional array duplicates
Copy the Code code as follows:
/**
* Remove duplicates from two-dimensional arrays
* @param $array Array
* @param the key of the field when $keyArray restore
* @return array of duplicates is removed
*/
Public Function ARRAY_UNIQUE_FB ($array, $keyArray) {
$temp =array ();
foreach ($array 2D as $v) {
$v = Join (",", $v); Dimension, you can also convert one-dimensional array to a comma-concatenated string using implode
$temp [] = $v;
}
$temp = Array_unique ($temp); Remove duplicate strings, that is, duplicate one-dimensional arrays
foreach ($temp as $k = = $v) {
$temp [$k] = Explode (",", $v); Re-assemble the disassembled array
$temp [$k]= array_combine ($keyArray, Explode (",", Trim ($v)));
}
return $temp;
}
$testArray =ARRAY_UNIQUE_FB (Array (' A ' =>1, ' B ' =>2, ' C ' =>3),
Array (' A ' =>1, ' B ' =>2, ' C ' =>3), Array (' A ' =>1, ' B ' =>2, ' C ' =>3) '), Array (' A ', ' B ', ' C '));
Print_r ($testArray);
http://www.bkjia.com/PHPjc/760290.html www.bkjia.com true http://www.bkjia.com/PHPjc/760290.html techarticle The bubble sort copy Code code is as follows://bubble sort function Bubble_sort ($array) {$count =count ($array), if ($count = 0) {return false;} for ($i =0 ; $i $count; $i + +) {for ($j =0;