Home > Backend Development > PHP Tutorial > Example of implementing two-dimensional array quick sorting algorithm in PHP

Example of implementing two-dimensional array quick sorting algorithm in PHP

黄舟
Release: 2023-03-16 17:10:02
Original
1978 people have browsed it

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][&#39;score&#39;]; 
    $leftarray = array (); 
    $rightarray = array (); 
    for($i = 1; $i < count ( $data ); $i ++) { 
      if ($data [$i][&#39;score&#39;] <= $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(&#39;sid&#39;=>1,&#39;score&#39;=>76), 
  array(&#39;sid&#39;=>2,&#39;score&#39;=>93), 
  array(&#39;sid&#39;=>3,&#39;score&#39;=>68.5), 
  array(&#39;sid&#39;=>4,&#39;score&#39;=>82.5), 
  array(&#39;sid&#39;=>5,&#39;score&#39;=>60.5) 
); 
print_r(Bubble::main($arr));
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template