本文实例讲述了PHP简单实现冒泡排序的方法。分享给大家供大家参考,具体如下:
PHP;">
PHP
$files = array("file11.txt","file22.txt","file1.txt","file2.txt");
function mySort($arr,$nat=false) {
for($i=0;$i0) {
$tmp = $arr[$j];
$arr[$j] = $arr[$j+1];//从小到大排
$arr[$j+1] = $tmp;
}
} else {
//按照strcmp()自然排序函数
if(strcmp($arr[$j],$arr[$j+1])>0) {
$tmp = $arr[$j];
$arr[$j] = $arr[$j+1];//从小到大排
$arr[$j+1] = $tmp;
}
}
}
}
return $arr;
}
//Array ( [0] => file1.txt [1] => file11.txt [2] => file2.txt [3] => file22.txt )
print_r(mySort($files,false));
echo "
"; //2<11 //Array ( [0] => file1.txt [1] => file2.txt [2] => file11.txt [3] => file22.txt ) print_r(mySort($files,true)); ?>
"; //2<11 //Array ( [0] => file1.txt [1] => file2.txt [2] => file11.txt [3] => file22.txt ) print_r(mySort($files,true)); ?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://www.f2er.com/php/18198.html