• php算法


     1 //插入排序(一维数组)
     2 function insert_sort($arr){
     3     $count = count($arr);
     4     for($i=1; $i<$count; $i++){
     5         $tmp = $arr[$i];
     6         $j = $i - 1;
     7         while($arr[$j] > $tmp){
     8             $arr[$j+1] = $arr[$j];
     9             $arr[$j] = $tmp;
    10             $j--;
    11          }
    12      }
    13     return $arr;
    14 }
     1 //选择排序(一维数组)
     2 function select_sort($arr){
     3     $count = count($arr);
     4     for($i=0; $i<$count; $i++){
     5         $k = $i;
     6         for($j=$i+1; $j<$count; $j++){
     7             if ($arr[$k] > $arr[$j])
     8                 $k = $j;
     9             if ($k != $i){
    10                 $tmp = $arr[$i];
    11                 $arr[$i] = $arr[$k];
    12                 $arr[$k] = $tmp;
    13              }
    14          }
    15      }
    16     return $arr;
    17 }
     1 //快速排序(一维数组) 
     2 function quick_sort($array){
     3   if (count($array) <= 1) return $array; 
     4   $key = $array[0];
     5   $left_arr = array();
     6   $right_arr = array();
     7   for ($i=1; $i<count($array); $i++){
     8     if ($array[$i] <= $key)
     9       $left_arr[] = $array[$i];
    10     else
    11       $right_arr[] = $array[$i];
    12   }
    13   $left_arr = quick_sort($left_arr);
    14   $right_arr = quick_sort($right_arr); 
    15   return array_merge($left_arr, array($key), $right_arr);
    16 }
     1 //冒泡排序(一维数组)
     2 function bubble_sort($array){
     3     $count = count($array);
     4     if ($count <= 0) return false;
     5     for($i=0; $i<$count; $i++){
     6         for($j=$count-1; $j>$i; $j--){
     7             if ($array[$j] < $array[$j-1]){
     8                 $tmp = $array[$j];
     9                 $array[$j] = $array[$j-1];
    10                 $array[$j-1] = $tmp;
    11              }
    12          }
    13      }
    14     return $array;
    15 } 
     1 //二分查找(递归)
     2 function bin_search($arr,$low,$high,$value) {
     3     if($low>$high)
     4         return false;
     5     else {
     6         $mid=floor(($low+$high)/2);
     7         if($value==$arr[$mid])
     8             return $mid;
     9         elseif($value<$arr[$mid])
    10             return bin_search($arr,$low,$mid-1,$value);
    11         else
    12             return bin_search($arr,$mid+1,$high,$value);
    13     }
    14 }
     1 //二分查找(非递归)
     2 function bin_search($arr,$low,$high,$value) {
     3     while($low<=$high) {
     4         $mid=floor(($low+$high)/2);
     5         if($value==$arr[$mid])
     6             return $mid;
     7         elseif($value<$arr[$mid])
     8             $high=$mid-1;
     9         else
    10             $low=$mid+1;
    11     }
    12     return false;
    13 }
     1 //2.牛年求牛:有一母牛,到4岁可生育,每年一头,所生均是一样的母牛,到15岁绝育,不再能生,20岁死亡,问n年后有多少头牛
     2 function niunum($n) {
     3 static $num = 1;
     4 for ($i = 1; $i <= $n; $i++) {
     5    if ($i >= 4 && $i < 15) {
     6     $num++;
     7     niunum($n - $i);
     8    }
     9    if ($i == 20) $num--;
    10 }
    11 return $num;
    12 }
    13 echo niunum(10);
    1 //3.合并多个数组,不用array_merge(),思路:遍历每个数组,重新组成一个新数组。
    2 function unionArray($a, $b) {
    3 $re = array();
    4 foreach ($a as $v) $re[] = $v;
    5 foreach ($b as $v) $re[] = $v;
    6 return $re;
    7 }
    8 print_r(unionArray(array(1,2,4,5,'s'), array(2,5,7,'c','d')));
    1 //8.把数组array(12,34,56,32) 转化为 array(1,2,3,4,5,6,3,2)
    2 function changeArr($arr) {
    3 return str_split(implode('', $arr));
    4 }
    5 print_r(changeArr(array(12,34,56,32)));
     1 //9.把数字1-1亿换成汉字表述,如:123->一百二十三
     2 function intToCnstr($intval) {
     3 $cnNum = array('零','一','二','三','四','五','六','七','八','九');
     4 $cnUnit = array('','十','百','千','万','亿');
     5 $reCnStr = '';
     6 $intval = intval($intval);
     7 if ($intval < 10 && $intval >= 0) {
     8    $reCnStr .= $cnNum[$intval];
     9 } elseif ($intval == 1000000000) {
    10    $reCnStr .= $cnNum[1].$cnUnit[5];
    11 } elseif ($intval < 0 || $intval > 1000000000) {
    12    $reCnStr .= '';
    13 } else {
    14    $str = strval($intval);
    15    $len = strlen($str);
    16    for ($i = 0; $i < $len; $i++) {
    17     if (intval($str{$i}) != 0) {
    18      $reCnStr .= $cnNum[intval($str{$i})];
    19      $j = $len - 1 - $i;
    20      if ($j < 5) {
    21       $reCnStr .= $cnUnit[$j];
    22      } elseif ($j >=5 && $j <= 8) {
    23       $reCnStr .= $cnUnit[$j - 4];
    24      }
    25     } else {
    26      if ($i > 0 && $str{$i} != $str{$i - 1}) $reCnStr .= $cnNum[0];
    27     }
    28    }
    29 }
    30 return $reCnStr;
    31 }
    32 echo intToCnstr(9912016);
  • 相关阅读:
    Java study 1:The note of studying Socket which based UDP
    关于Meta标签中formatdetection属性及含义
    编辑sass报错:error style.scss (Line 3: Invalid GBK character "\xE5")解决办法
    比较三个 CSS 预处理器:Sass、LESS 和 Stylus(下)
    vue引入bootstrap、jquery
    Something about SeekingJobResume简历
    Something about SeekingJobTelInterview(电话面试)
    此时彼时
    The method getTextContent() is undefined for the type Node
    单例模式实现方式
  • 原文地址:https://www.cnblogs.com/j-king/p/3747655.html
Copyright © 2020-2023  润新知