Home  >  Article  >  Backend Development  >  PHP bubble sort, quick sort, quick search, two-dimensional array deduplication example sharing_PHP tutorial

PHP bubble sort, quick sort, quick search, two-dimensional array deduplication example sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:59775browse


1. Bubble sort

Copy code The 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] ){
                                                                                                                array[$j+1]=$temp;
                                                                                                           .
2. Quick sort




Copy code
The code is as follows:

//Quick sort

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 "
";


3. Quickly find the position where the value first appears




Copy code

The code is as follows:

/**
* Quickly find the position where the value first appears
* @param array $array Array
* @param string $k The value to be found
* @param int $low The smallest key of the search range Value
* @param int $high The maximum key value of the range
*/
function search($array, $k, $low=0, $high=0)
{
// Determine whether it is 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);
//If If found, return
if ($array[$mid] == $k){
return $mid;
}
//If not found, continue to search
elseif ($k " k, $mid+1, $high);
}
}
return -1;
}
$array = array(4,5,7,8,9,10, ;
4. Remove duplicate items from the two-dimensional array



Copy the code

The code is as follows:


/** * Remove duplicates in the two-dimensional array * @param $array2D Array * @param $keyArray The key corresponding to the field when restoring 🎜 >*/ public function array_unique_fb($array2D,$keyArray){
$temp=array();
foreach ($array2D as $v){

$v = join(",",$v); //For dimensionality reduction, you can also use implode to convert the one-dimensional array into a string connected with commas

$temp = array_unique($temp); //Remove duplicate strings, that is, repeated one-dimensional arrays
foreach ($temp as $k => $v){
//$ temp[$k] = explode(",",$v); //Reassemble the disassembled array
$temp[$k]= array_combine($keyArray ,explode(",",trim($ v)));
}
return $temp;
}



$testArray=array_unique_fb(array(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);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/760290.htmlTechArticle1. Bubble sort copy code The 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;...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn