Home > Backend Development > PHP Tutorial > PHP two-dimensional array quick sorting algorithm_PHP tutorial

PHP two-dimensional array quick sorting algorithm_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:32:22
Original
881 people have browsed it

The basic theory of the two-dimensional array sorting algorithm and the one-dimensional array sorting algorithm are the same. Both small values ​​are placed in the array on the left through comparison, and large values ​​are placed in the array on the right and recursed respectively.

<?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));
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/755778.htmlTechArticleThe basic theory of the two-dimensional array sorting algorithm and the one-dimensional array sorting algorithm are the same, both are to compare the small ones Put it in the array that changes on the left, and put the larger one in the array on the right and recurse separately. ...
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