- <?php
- //数组的排序
- //排序分为内部排序和外部排序
- //数据全部在内存中的排序叫内部排序
- //数据量太大的话,无法全部加载到内存中,需要借助外部存储设备进行排序,叫做外部排序
- //内部排序分类:1,冒泡排序2,快速排序3,选择排序;4,
- $arr=array(3,1,5,7,3,123,54);
- print_r($arr);
- echo "<br/>";
- //冒泡排序
- function sortM(&$a){
- $l=count($a);
- for($x=1;$x<$l;$x++){
- for($y=$x;$y<$l;$y++){
- if($a[$y]<$a[$y-1]){
- $temp=$a[$y];
- $a[$y]=$a[$y-1];
- $a[$y-1]=$temp;
- }
- }
- }
- }
- //sortM($arr);
- //选择排序
- function selectSort(&$arr){
- $l=count($arr);
- for($v=0;$v<$l;$v++){
- $minIndex=$v;
- for($x=$v;$x<$l;$x++){
- if($arr[$x]<$arr[$minIndex])$minIndex=$x;
- }
- $temp=$arr[$minIndex];
- $arr[$minIndex]=$arr[$v];
- $arr[$v]=$temp;
- }
- }
- //selectSort($arr);
- //插入排序
- function insertSort(&$arr){
- for($v=1;$v<count($arr);$v++){
- //准备要插入的数据
- $temp = $arr[$v];
- $index=$v-1;
- while($index>=0){
- if($arr[$index]>$temp){
- $arr[$index+1]=$arr[$index];
- $index--;
- }else{
- $arr[$index+1]=$temp;
- break;
- }
- }
- if($index==-1)
- $arr[0]=$temp;
- }
- }
- insertSort($arr);
- print_r($arr);
- ?>
转载于:https://blog.51cto.com/soukenan/1070603