关于php中 usort 函数排序问题
基本可以断定是快速排序,第一次的基准元素选取是元素的最中间元素,现在问题来了,当进行第一次分组后,得到2组数据,这时还要进行基准元素的选取,而这时选取基准元素到底是什么规则呢, 经过分析 好像不是随机,因为每次都是固定的, 也不是 平衡选中间值 也不再是选择中点元素,请高人指点,在此谢谢每一位回答的朋友
下面给出一组测试数据
$a = array(array('key1' => 588,'key2' => 'blah'),
array('key1' => 800 ,'key2' => 'this'),
array('key1' => 511,'key2' => 'that'),
array('key1' => 611,'key2' => 'nhat'),
array('key1' => 400,'key2' => 'mhat'),
array('key1' => 201,'key2' => 'mhat'),
array('key1' => 301,'key2' => 'mhat'),
array('key1' => 101,'key2' => 'mhat'),
array('key1' => 701,'key2' => 'mhat')
);
function mysort1($x,$y)
{
echo $x['key1'].' vs '.$y['key1'];
echo "<br>";
echo "<br>";
return ($x['key1'] > $y['key1']);
}
usort($a,'mysort1');
print_r($a);
页面输出:
400 vs 800
701 vs 400
101 vs 400
400 vs 511
301 vs 400
400 vs 611
201 vs 400
588 vs 400
101 vs 201
301 vs 101
201 vs 101
301 vs 201
511 vs 611
701 vs 511
800 vs 511
588 vs 511
611 vs 511
588 vs 611
701 vs 588
800 vs 588
611 vs 588
800 vs 611
701 vs 800
611 vs 701