/
InsertSort.php
45 lines (40 loc) · 1.04 KB
/
InsertSort.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* Class InsertSort
*
* 插入排序
*
* 遍历序列,将当前值插入到之前排序好的序列的合适位置.
*
*/
class InsertSort
{
private $originalData = [];
private $resultData = [];
private $dataLen;
public function __construct($original = [])
{
$this->originalData = $original;
$this->resultData = $original;
$this->dataLen = count($original);
}
public function sort()
{
for ($i = 0; $i < $this->dataLen; $i++) {
for ($j = $i; $j > 0; $j--) {
if ($this->resultData[$j-1] > $this->resultData[$j]) {
$tmp = $this->resultData[$j-1];
$this->resultData[$j-1] = $this->resultData[$j];
$this->resultData[$j] = $tmp;
} else {
break;
}
}
}
return $this->resultData;
}
}
$testData = [155, 19, 88, 12, 40, 29, 1, 23];
$insertSort = new InsertSort($testData);
echo '<pre>';
var_dump($insertSort->sort());