Php bubble Sort, quick sort, quick lookup, two-dimensional array to _php instance sharing

Source: Internet
Author: User


First, bubble sort

Copy Code code 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, fast sorting

Copy Code code as follows:

Quick Platoon
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 "<br/>";
Print_r (Quick_sort ($myarray));
echo "<br/>";

Three, quick find the value of the first occurrence of the position

Copy Code code as follows:

/**
* Quick Find where the value first appears
* Array $array @param array
* @param string $k The value to be found
* @param int $low The minimum key value for the lookup range
* @param the maximum key value of an int $high range
*/
function Search ($array, $k, $low =0, $high =0)
{
Determines whether the first call
if (count ($array)!=0 and $high = = 0) {
$high = count ($array);
}
If there are remaining array elements
if ($low <= $high) {
Take the middle value of $low and $high
$mid = Intval (($low + $high)/2);
Returns if it is found
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 search function
Echo Search ($array, 8); Call the search function and output the search results

Four, remove the two-dimensional array duplicates

Copy Code code as follows:

/**
* Remove duplicates from two-dimensional arrays
* @param $array 2d Array
* @param key for field when $keyArray restore
* @return Array to remove duplicates
*/
Public Function ARRAY_UNIQUE_FB ($array 2d, $keyArray) {
$temp =array ();
foreach ($array 2D as $v) {
$v = Join (",", $v); dimensionality reduction, you can also use implode to convert a one-dimensional array into a comma-connected string
$temp [] = $v;
}
$temp = Array_unique ($temp); Remove the duplicate string, which is a repeating one-dimensional array
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.