Home > Backend Development > PHP Tutorial > php-insertion sort

php-insertion sort

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-08 09:29:56
Original
1154 people have browsed it
$list = array(10,3,5,7,18,11,45,64,74,23,21,6);
$list = insert_sort($list);
var_dump($list);

function insert_sort($array){
	$return = array();
	for ($i=0,$count=count($array); $i < $count; $i++) {
		$last = true;
		for ($j=0,$size=count($return); $j < $size; $j++) { 
			if($return[$j] > $array[$i]){
				$last= false;				
				$m = $size;
				while($m > $j){
					$return[$m] = $return[$m-1];
					$m--;
				}
				$return[$j] = $array[$i];
				break;
			}
		}
		if($last){
			$return[] = $array[$i];
		}
	}
	return $return;
}




function insertsort($arr1,$max=11){   
       
    for($i=1;$i<=$max;$i++){   
        $tmp = $arr1[$i];   
        $j = $i - 1;   
        while($j>=0 && $tmp<$arr1[$j]){   
            $arr1[$j+1] = $arr1[$j];   
            $j--;   
        }   
        $arr1[$j+1] = $tmp;   
    }   
       
    return $arr1;
}   
Copy after login

The above introduces php-insertion sort, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Return value problem
From 1970-01-01 08:00:00
0
0
0
Return JSON response via PHP
From 1970-01-01 08:00:00
0
0
0
Usage of return
From 1970-01-01 08:00:00
0
0
0
Why not return data in json format?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template