vlambda博客
学习文章列表

PHP 插入排序 - 直接插入排序

<?php
function microtime_float(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec);}
//TODO : 插入排序 - 直接插入排序/*最好的情况 : T = O(N)最坏的情况 : T = O(N^2)稳定 */function InsertionSort($data, $L){ $time_start = microtime_float(); for ($i = 1; $i < $L; $i++) { for ($j = $i; $j > 0 && $data[$j] < $data[$j - 1]; $j--) { list($data[$j], $data[$j - 1]) = [$data[$j - 1], $data[$j]]; } } $time_end = microtime_float();
echo "InsertionSort - Time:" . ($time_end - $time_start) . "s" . "<br>"; return $data;}
function main(){ //TODO : 生成数据 $number = []; for ($i = 0; $i < 10000; $i++) { $number[] = rand(0, 1000000000); }
// echo "<pre>";// print_r($number);// print_r(InsertionSort($number, count($number)));
InsertionSort($number, count($number));}
main();