// 冒泡法 function bubbleSort($array) { !is_array($array) ? '非数组' : ''; $len = count($array); for ($i = 0; $i < $len; $i++) { for ($j = $len -1 ; $j > $i; $j--) { if ($array[$j] > $array[$j-1]) { $temp = $array[$j-1]; $array[$j-1] = $array[$j]; $array[$j] = $temp; } } } return $array; } $a = array(1, 5, 2, 7, 15, 12, 70, 100, 60); print_r(bubbleSort($a)); echo '<br />'; // 二分查找法 假设$array 是从小到大的线性数组 function binSearch($array, $findvalue) { $low = 0; $high = count($array) - 1; while ($low <= $high) { $mid = intval(($low + $high) / 2); if ($array[$mid] == $findvalue) { return $mid; } elseif ($array[$mid] > $findvalue) { $high = $mid - 1; } else { $low = $mid + 1; } } return false; } $b = array(1, 2, 3, 4, 56, 70, 121, 300, 500); echo binSearch($b, 121); echo '<br />'; // 16进制转10进制 内置函数 hexdec function hexToDec($H) { $array = array('F' => 15, 'E' => 14, 'D' => 13, 'C' => 12, 'B' => 11, 'A' => 10); $len = strlen($H); $sum = 0; for ($i = 0; $i < $len; $i++) { $bit = substr($H, $i, 1); if (!is_numeric($bit)) { $bit = $array[strtoupper($bit)]; } $e = $len - $i - 1; $sum += pow(16, $e) * $bit; } return $sum; } echo hexToDec('ff1'); echo '<br />'; // 选择排序法 function selectSort($array) { !is_array($array) ? '非数组' : ''; $len = count($array); for ($i = 0; $i < $len; $i++) { for ($j = $i + 1; $j < $len; $j++) { if ($array[$j] > $array[$i]) { $temp = $array[$i]; $array[$i] = $array[$j]; $array[$j] = $temp; } } } return $array; } $a = array(2,5,1,6,11,8,15,9); print_r(selectSort($a)); //快速排序(数组排序) function quicksort($array) { if (count($array) <= 1) return $array; $key = $array[0]; $left_arr = array(); $right_arr = array(); for ($i = 1; $i < count($array); $i++) { if ($array[$i] <= $key) { $left_arr[] = $array[$i]; } else { $right_arr[] = $array[$i]; } } //运用递归 $left_arr = quicksort($left_arr); $right_arr = quicksort($right_arr); return array_merge($left_arr, array($key), $right_arr); } $a = array(1,3,7,2,5,10,9,21); print_r(quicksort($a)); // 求素数 // 方法一:直接法 $a = 100000; $array = array(); $array[] = 1; $array[] = 2; for ($i = 3; $i <= $a; $i++) { $flags = true; if($i % 2 == 0 || $i % 3 == 0 || $i % 5 == 0){ continue; } for ($j = 2; $j < $i; $j++) { if ($i % $j == 0) { $flags = false; break; } } if(!$flags){ continue; } $array[] = $i; } //print_r($array); // 方法二:优化法 function prime5($MAX) { $data[0] = 2; $total = 1; for ($i = 3; $i <= $MAX; $i += 2) { $sqrt = ceil(sqrt($i)); $isp = true; for ($j = 0; $j < $total && $data[$j] <= $sqrt; $j++) { if ($i % $data[$j] === 0) { $isp = false; break; } } if ($isp) { //$total = $total + 1; $data[$total] = $i; $total = $total + 1; } } return $data; } $a = prime5(20000); print_r($a);
<?php//冒泡排序function maopao_sort($demo){ $num = count($demo); for($i=0;$i<$num;$i++){ for($j=$num-1;$j>$i;$j--){ if($demo[$j]<$demo[$j-1]){ $temp = $demo[$j]; $demo[$j]=$demo[$j-1]; $demo[$j-1]= $temp; } } } return $demo;}//插入排序function charu_sort($demo){ $num = count($demo); for($i=1;$i<$num;$i++){ $temp=$demo[$i]; $dqweizhi = $i-1;//记录当前位置 while(($dqweizhi>=0)&&($temp<$demo[$dqweizhi])){ $demo[$dqweizhi+1] = $demo[$dqweizhi]; $dqweizhi--; } $demo[$dqweizhi+1] = $temp; } return $demo;}//选择法排序function select_sort($demo){ $num = count($demo); for($i=0;$i<$num-1;$i++){ $temp=$demo[$i]; $dqweizhi=$i; for($j=$i+1;$j<$num;$j++){ if($demo[$j]<$temp){ $temp=$demo[$j]; $dqweizhi=$j; } } $demo[$dqweizhi]=$demo[$i]; $demo[$i]=$temp; } return $demo;}//快速排序function quick_sort($demo){$num = count($demo);if($num<=1){ return $demo;}$key=$demo[0];$left_array=array();$right_array=array();for($i=1;$i<$num;$i++){ if($demo[$i]<=$key){ $left_array[]=$demo[$i]; }else{ $right_array[]=$demo[$i]; }}$left_array =quick_sort($left_array);$right_array=quick_sort($right_array);return array_merge($left_array,array($key),$right_array);}$test = array('43','154','3','78','13','284','167','2','56','2234','121','57','345');$sss = quick_sort($test);var_dump($sss);?>