Implementation code of php two dimensional array quick sorting algorithm

  • 2021-08-10 06:53:15
  • OfStack

Implementation Code of php 2-D Array Quick Sorting Algorithm

The basic theory of 2-D array sorting algorithm and 1-D array sorting algorithm is one, which is to put small values in the left-changed array and large values in the right-hand array recursively.

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

If you have any questions, please leave a message or go to this site community to exchange and discuss, thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: