一道PHP笔试题:请使用伪语言结合数据结构冒泡排序法对以下一组数据进行排序 10 2 36 14 10 25 23 85 99 45。
关于冒泡排序大家应该都非常熟悉了,原理就不多说了,这里只做简单记录。下面是参考代码。
array ('fid' => 1, 'tid' => 1, 'name' => 'Name1' ), '1' => array ('fid' => 1, 'tid' => 2 , 'name' => 'Name2' ), '2' => array ('fid' => 1, 'tid' => 5 , 'name' => 'Name3' ), '3' => array ('fid' => 1, 'tid' => 7 , 'name' => 'Name4' ), '4' => array ('fid' => 3, 'tid' => 9, 'name' => 'Name5' ) ); $arr2 = array(); foreach ($arr1 as $key => $value) { $arr2[$value['fid']][] = array('tid' => $value['tid'],'name' => $value['name']); } return $arr2; } //print_r($arr2); //$eString = 'open_door'; public function getString($eString = null) { $eString = explode('_',$eString); $eString = array_map("ucfirst",$eString); $eString = implode($eString,''); return $eString; } /** * 冒泡排序 * @return unknown */ public function getBubble() { $isOver = false; $bubbleArray = array(10,2,36,14,10,25,23,85,99,45); $bubbleResult = $bubbleArray; do{ $bubbleArray = $bubbleResult; $isOver = true; foreach ($bubbleArray as $key => $value) { if ($value <$bubbleResult[$key-1]) { $bubbleResult[$key]=$bubbleResult[$key-1]; $bubbleResult[$key-1]=$value; $isOver = false; } } }while (!$isOver); return $bubbleResult; } } $engage = new engage(); echo ''; print_r($engage->getArray()); echo ''; echo $engage->getString('make_by_nowamagic'); echo ''; print_r($engage->getBubble()); echo ''; ?>
程序运行结果如下:
Array ( [1] => Array ( [0] => Array ( [tid] => 1 [name] => Name1 ) [1] => Array ( [tid] => 2 [name] => Name2 ) [2] => Array ( [tid] => 5 [name] => Name3 ) [3] => Array ( [tid] => 7 [name] => Name4 ) ) [3] => Array ( [0] => Array ( [tid] => 9 [name] => Name5 ) ) ) MakeByNowamagic Array ( [0] => 2 [1] => 10 [2] => 10 [3] => 14 [4] => 23 [5] => 25 [6] => 36 [7] => 45 [8] => 85 [9] => 99 )
本文地址:http://www.nowamagic.net/librarys/veda/detail/1559,欢迎访问原出处。