Home > Article > Backend Development > Example of implementing two-dimensional array quick sorting algorithm in PHP
This article mainly introduces the relevant information about the implementation code of PHP two-dimensional array quick sorting algorithm. I hope this article can help everyone to realize such a function. Friends in need can refer to it
php Implementation code of two-dimensional array quick sorting algorithm
The two-dimensional array sorting algorithm and the one-dimensional array sorting algorithm have the same basic theory, both put the small value on the left through comparison In the variable array, large values are placed in the array on the right and recursed separately.
Example code:
<?php class Bubble { private function __construct() { } private static function sortt($data) { if (count ( $data ) <= 1) { return $data; } $tem = $data [0]['score']; $leftarray = array (); $rightarray = array (); for($i = 1; $i < count ( $data ); $i ++) { if ($data [$i]['score'] <= $tem ) { $leftarray[] = $data[$i]; } else { $rightarray[] = $data[$i]; } } $leftarray=self::sortt($leftarray); $rightarray=self::sortt($rightarray); $sortarray = array_merge ( $leftarray, array ($data[0]), $rightarray ); return $sortarray; } public static function main($data) { $ardata = self::sortt ( $data ); return $ardata; } } $arr=array( array('sid'=>1,'score'=>76), array('sid'=>2,'score'=>93), array('sid'=>3,'score'=>68.5), array('sid'=>4,'score'=>82.5), array('sid'=>5,'score'=>60.5) ); print_r(Bubble::main($arr));
The above is the detailed content of Example of implementing two-dimensional array quick sorting algorithm in PHP. For more information, please follow other related articles on the PHP Chinese website!