function m_order($arr){
for($i=0;$i<count($arr)-1;$i++){
for($j=0; $j<count($arr)-$i-1;$j++){
if($arr[$j]>$arr[$j+1]){
list($arr[$j], $arr[$j+1]) = array($arr[$j+1], $arr[$j]);
}
}
}
return $arr;
}
var_dump(m_order([1, 3, 2, 5, 7, 9]));die;
优化冒泡排序
function m_order($arr)
{
$count = count($arr);
if ($count <= 1) {
return $arr;
}
for ($h = 0; $h < $count - 1; $h++) {
$flag = 0;
for ($i = 0; $i < $count - $h - 1; $i++) {
if ($arr[$i] > $arr[$i + 1]) {
$flag = 1;
list($arr[$i], $arr[$i + 1]) = array($arr[$i + 1], $arr[$i]);
}
}
if ($flag == 0) {
return $arr;
}
}
return $arr;
}
var_dump(m_order([2, 3, 4, 5, 1, 7, 9]));die;
/**
* 遍历目录
* @param $dir
* @return array
*/
function b_list($dir){
$dir .= substr($dir,-1) == '/'?'':'/';
echo $dir;
$listArr = [];
foreach(glob($dir."*") as $v){
$listArr[] = $v;
if(is_dir($v)){
$listArr = array_merge($listArr,b_list($v));
}
}
return $listArr;
}
var_dump(b_list('E:/shen'));die;
class Myqueue{
private $queue = [];
public function push($params){
array_push($this->queue, $params);
}
public function pop(){
array_pop($this->queue);
}
public function shift(){
array_shift($this->queue);
}
public function unshift($params){
array_unshift($this->queue, $params);
}
public function getQueue(){
return $this->queue;
}
}
$obj = new Myqueue();
$obj->push(1);
$obj->push(2);
$obj->push(3);
$obj->push(4);
$obj->pop();
$obj->shift();
$obj->unshift(7);
$obj->unshift(8);
$obj->unshift(9);
var_dump($obj->getQueue());die;
/**
* 数猴子 找大王
* @param $n 总数
* @param $m 踢数
* @return mixed
*/
function monkey($n, $m){
$arr = range(1, $n);
$i = 0;
while(count($arr)>1){
$i+=1;
$head = array_shift($arr);
if($i % $m != 0){
array_push($arr, $head);
}
}
return $arr[0];
}
echo monkey(10, 5);die;
/**
* 斐波那契数列
* @param $n
* @return mixed
*/
function fbnq($n){
$arr[1] = 1;
$arr[2] = 1;
for($i=3; $i<=$n; $i++){
$arr[$i] = $arr[$i-1] + $arr[$i-2];
}
return $arr;
}
var_dump(array_values(fbnq(10)));die;
/**
* 自定义二维数组按照某个键值排序
* @param $arr
* @param $key
* @param $order
* @return array
*/
function myOrder($arr, $key, $order){
$keyArr = [];
foreach($arr as $k=>$v){
$keyArr[$k] = $v[$key];
}
if(strcasecmp($order, 'asc') == 0){
asort($keyArr);
}
if(strcasecmp($order, 'desc') == 0){
arsort($keyArr);
}
$newArr = [];
foreach($keyArr as $k=>$v){
$newArr[$k] = $arr[$k];
}
return $newArr;
}
$arr = [
[
'name'=>'shen25',
'age'=>'25',
],
[
'name'=>'shen27',
'age'=>'27',
],
[
'name'=>'shen24',
'age'=>'24',
],
[
'name'=>'shen26',
'age'=>'26',
],
[
'name'=>'shen30',
'age'=>'30',
],
];
var_dump(myOrder($arr, 'age', 'desc'));die;
/**
* 顺序查找
* @param $arr
* @param $params
* @return int|string
*/
function findParams($arr, $params){
foreach($arr as $k=>$v){
if($v == $params){
return $k;
}
}
return -1;
}
$arr = [1, 3, 2, 5, 123, 4, 54, 2];
var_dump(findParams($arr, 123));die;
/**
* 二分查找 $arr需排好序
* @param $arr
* @param $start
* @param $end
* @param $params
* @return mixed
*/
function erFen($arr, $start, $end, $params){
$key = intval(($start+$end)/2);
if($arr[$key] == $params){
return $key;
}elseif($arr[$key] < $params){
return erFen($arr, $key+1,$end,$params);
}else{
return erFen($arr, $start,$key-1,$params);
}
return false;
}
$b = '/a/b/c/d/e.php';
$a = '/a/b/f/g/j/h.php';
function diff($a, $b){
$aArr = explode('/',$a);
$bArr = explode('/',$b);
$bCount = count($bArr);
$k = 0;
for($i=0;$i<$bCount;$i++){
if($aArr[$i] != $bArr[$i]){
$k = $i;
break;
}
}
//echo $k;die;
//var_dump(array_fill(0, $bCount-$k, '..'));die;
$result = array_merge(array_fill(1, $bCount-$k-1, '..'), array_slice($aArr,$k));
return implode('/', $result);
}
var_dump(diff($a, $b));die;
/**杨辉三角
* @param $n
*/
function yhsj($n){
$arr=array();
for($i=1;$i<=$n;$i++){
for($j=1;$j<=$i;$j++){
if($j==1||$j==$i){
echo $arr[$i][$j]=1;
}else{
echo $arr[$i][$j]=$arr[$i-1][$j-1]+$arr[$i-1][$j];
}
echo " ";
}
echo "
";
}
}
var_dump(yhsj(5));die;
附加:创建链表 以及翻转链表
class Node{
public $data;
public $next;
}
function createList(){
$list = new Node();
$list->next = null;
for($i=1;$i<10;$i++){
$node = new Node();
$node->data = "aaa".$i;
$node->next = $list->next;
$list->next = $node;
}
return $list;
}
function reserveList($list){
$old = $list->next;
$temp = null;
$new = null;
while($old!=null){
$temp = $old->next;
$old->next = $new;
$new = $old;
$old = $temp;
}
$newList = new Node();
$newList->next = $new;
return $newList;
}
print_r(createList());
var_dump(reserveList(createList()));