挿入ソート-POP


githubアドレス:https://github.com/luolaifa000/phpStudyCode/blob/master/InsertSort.php
基本的な考え方は、すべての要素が挿入されるまで、前の順序付けされた順序付けされた順序付けされた順序付けされた順序付けされたシーケンスに、順序付けされるレコードをステップごとに挿入することです.
 
$val)
    {
        echo '
';
        print_r($val);
        echo '
';
}
}
function prend()
{
$data = func_get_args();
foreach($data as $key=>$val)
{
echo '
';
        print_r($val);
        echo '
';
}
exit();
}
/**
*ソートの
* な え は、すべての が されるまで、 の けされた けされた けされた けされた けされた けされた けされたレコードにステップごとに することです.
* @author yumancang
*
*/
class InsertSort
{
/**
*
* @var array
*/
public $data = [];
public function __construct($array)
{
$this->data = $array;
}
/**
*
* の さn-1 n-2 n-3....1 (1+n)*n/2 = O(n2)
*/
public function ascInsertSort()
{
pre($this->data);
$length = count($this->data);
for ($i = 1; $i< $length; $i++) {
$temp = $this->data[$i];
for ($j = 0; $j < $i; $j++) {
if ($this->data[$i] < $this->data[$j]) {
for ($n = $i; $n>$j; $n--) {
$this->data[$n] = $this->data[$n-1];
}
break;
}
}
$this->data[$j] = $temp;
}
pre($this->data);
}
/**
*
* の さn-1 n-2 n-3....1 (1+n)*n/2 = O(n2)
*/
public function descInsertSort()
{
pre($this->data);
$length = count($this->data);
for ($i = 1; $i< $length; $i++) {
$temp = $this->data[$i];
for ($j = 0; $j < $i; $j++) {
if ($this->data[$i] > $this->data[$j]) {
for ($n = $i; $n>$j; $n--) {
$this->data[$n] = $this->data[$n-1];
}
break;
}
}
$this->data[$j] = $temp;
}
pre($this->data);
}
}
$start = memory_get_usage();
$bubble = new InsertSort([7,3,2,4,6,9,5,55,77,88,3,55,77,4545,33]);
//$bubble->ascInsertSort();
$bubble->descInsertSort();
$end = memory_get_usage();
prend(($end-$start)/1024/1024);
?>