* 1. 冒泡排序
~~~
function bubbleSort($arr)
{
$count = count($arr);
for($i=1;$i<=$count-1;$i++){
$flag = false;
for($j=$count-1;$j>=$i;$j--){
if($arr[$j]<$arr[$j-1]){
$tem = $arr[$j];
$arr[$j] = $arr[$j-1];
$arr[$j-1] = $tem;
$flag = true;
}
}
if(!$flag){
break;
}
}
return $arr;
}
~~~
* 2. 快速排序
~~~
function quickSort($arr1)
{
$count = count($arr1);
if($count<=1) return $arr1;
$left = [];
$right = [];
$middle = $arr1[0];
for($i=1;$i<=$count-1;$i++){
if($arr1[$i]<=$middle){
$left[] = $arr1[$i];
}else{
$right[] = $arr1[$i];
}
}
return array_merge(quickSort($left),(array)$middle,quickSort($right));
}
~~~
* 3. 选择排序
~~~
function selectSort($arr)
{
$count = count($arr);
if($count<=1) return $arr;
for ($i=0;$i<$count-1;$i++){
$tem = $i;
for($j=$i+1;$j<$count;$j++){
if($arr[$tem]>$arr[$j]){
$tem = $j;
}
}
if($i != $tem){
$temArr = $arr[$i];
$arr[$i] = $arr[$tem];
$arr[$tem] = $temArr;
}
}
return $arr;
}
~~~
* 4. 插入排序
~~~
function insertArr(array $arr=[])
{
$count = count($arr);
if($count<=1) return $arr;
for($i=1;$i<$count;$i++){
$tem = $arr[$i];
for($j=$i-1;$j>=0&&$tem<$arr[$j];$j--){
$arr[$j+1] = $arr[$j];
$arr[$j] = $tem;
}
}
return $arr;
}
~~~
* 5. 归并排序
~~~
function mergeSort(array $arr=[])
{
$count = count($arr);
if ($count <= 1) return $arr;
$middle = ceil(($count) / 2);
$arr1 = array_chunk($arr, $middle);
$arrLeft = mergeSort($arr1[0]);
$arrRight = mergeSort($arr1[1]);
while (count($arrLeft) && count($arrRight)) {
if ($arrLeft[0] < $arrRight[0]) {
$arrMerge[] = array_shift($arrLeft);
} else {
$arrMerge[] = array_shift($arrRight);
}
}
return array_merge($arrMerge, $arrLeft, $arrRight);
}
~~~
* 6. 桶排序
~~~
function bucketSort($arr)
{
$min = min($arr);
$max = max($arr);
$bucket = array_fill($min,$max-$min+1,0);
foreach ($arr as $key => $item) {
$bucket[$item]++;
}
foreach ($bucket as $key => $v) {
for ($i=1;$i<=$v;$i++){
$merge[] = $key;
}
}
return $merge;
}
~~~
* 7. 堆排序
* 8. 基数排序
~~~
// 获取最大数的位数
function getNum($arr)
{
$high = (string)max($arr);
$num = strlen($high);
return $num;
}
// 主程序
function baseSort($arr)
{
$num = getNum($arr);
for ($i=1;$i<=$num;$i++){
$arr = dealSort($arr,$i);
}
return $arr;
}
function dealSort($arr,$loop)
{
$tempArr = array();
$count = count($arr);
for($i=0;$i<10;$i++){
$tempArr[$i] = [];
}
for($i=0;$i<$count;$i++){
$index = ($arr[$i]/pow(10,$loop-1))%10;
$tempArr[$index][] = $arr[$i];
}
$k = 0;
for($j=0;$j<=10;$j++){
while($tem = @array_shift($tempArr[$j])){
$arr[$k ++] = $tem;
}
}
return $arr;
}
~~~
* 9. 希尔排序
* 11. 二分查找
~~~
function halfSearch($arr,$search)
{
$count = count($arr);
$left = 0;
$right = $count -1;
if($search<$arr[$right] || $search > $arr[$left]) return false;
while ($left<=$right){
$middle = (int)ceil(($left+$right)/2);
if($search<$arr[$middle]){
$right = $middle-1;
}elseif($search>$arr[$middle]){
$left = $middle+1;
}else{
return $middle;
}
}
return false;
}
~~~
* 12. 顺序查找
~~~
function orderSearch($arr,$k)
{
foreach ($arr as $key=>$item) {
if($k == $item){
return $key;
}
}
return false;
}
~~~
* 13. 遍历一个文件夹下面的所有文件和子文件夹
~~~
function getAllDir($dir)
{
if (!is_dir($dir)) return false;
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
if (is_dir($dir . '/' . $file)) {
echo $file."\r\n";
getAllDir($dir . '/' . $file);
}else{
echo $file."\r\n";
}
}
}
}
}
~~~
* 14. 写一个二维数组排序函数,可以调用php内置函数,能够具有通用性