php-insertion sort

WBOY
Release: 2016-08-08 09:29:56
Original
976 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template