Skip to content

Commit ecceb13

Browse files
committed
希尔排序
1 parent da8e7fe commit ecceb13

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

PhpCodes/DataStructureAndAlgorithm/Sort/CompareSort/InsertSort/ShellSort.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,74 @@
44
*
55
* @author liu hao<[email protected]>
66
* @copyright liu hao<[email protected]>
7+
*
8+
* 希尔排序
9+
*
10+
* 1. 计算初始增量,一般开始的增量是序列长度/2
11+
* 2. 按增量分组
12+
* 3. 分组内排序
13+
* 4. 增量减小,增量每次减小到之前增量的1/2,重复1.2步骤,直至增量为一
14+
*
15+
*
16+
* 例如序列: 10, 3, 6, 9, 4
17+
* 1. 序列长度是5,所以初始增量是5/2 = 2
18+
* 2. 按增量为2分组
19+
* 1 => 10, 6, 4
20+
* 2 => 3, 9
21+
* 3. 分组内排序
22+
* 1 => 4, 6, 10
23+
* 2 => 3, 9
24+
* 序列变为: 4, 3, 6, 9, 10
25+
*
26+
* 4.增量减小到 2/2 = 1
27+
* 按增量为1分组:
28+
* 1 => 4, 3, 6, 9, 10
29+
* 分组内排序:
30+
* 1 => 3, 4, 6, 9, 10
31+
* 因增量已为1,所以得到排序序列: 3, 4, 6, 9, 10
32+
*
33+
*
734
*/
835

936
class ShellSort
1037
{
1138
private $originalData = [];
39+
private $resultData = [];
40+
private $dataLen;
1241

1342
public function __construct($original = [])
1443
{
1544
$this->originalData = $original;
45+
$this->resultData = $original;
46+
$this->dataLen = count($original);
1647
}
1748

1849
public function sort()
1950
{
20-
return [];
51+
$step = $this->dataLen / 2;
52+
while ($step >= 1) {
53+
for ($i = 0; $i < $step; $i++) {
54+
for ($j = $i; $j < $this->dataLen; $j += $step) {
55+
for ($z = $j; $z >= $step; $z -= $step) {
56+
if ($this->resultData[$z] < $this->resultData[$z - $step]) {
57+
$tmp = $this->resultData[$z - $step];
58+
$this->resultData[$z - $step] = $this->resultData[$z];
59+
$this->resultData[$z] = $tmp;
60+
} else {
61+
break;
62+
}
63+
}
64+
}
65+
}
66+
$step /= 2;
67+
}
68+
69+
return $this->resultData;
2170
}
2271
}
2372

24-
$shellSort = new ShellSort();
73+
$testData = [111, 34, 10, 22, 99, 89, 123, 44, 79];
74+
75+
$shellSort = new ShellSort($testData);
2576
echo '<pre>';
2677
var_dump($shellSort->sort());
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* code-segment
4+
*
5+
* @author liu hao<[email protected]>
6+
* @copyright liu hao<[email protected]>
7+
*
8+
* 简单选择排序
9+
* 1. 遍历剩余待排序序列,找出最值.
10+
* 2. 将最值和待排序序列的第一个元素交换.
11+
*/
12+
13+
class SimpleSelectSort
14+
{
15+
private $originalData = [];
16+
private $resultData = [];
17+
private $dataLen;
18+
19+
public function __construct($original = [])
20+
{
21+
$this->originalData = $original;
22+
$this->resultData = $original;
23+
$this->dataLen = count($original);
24+
}
25+
26+
public function sort()
27+
{
28+
for ($i = 0; $i < $this->dataLen - 1; $i++) {
29+
$tmp = $this->resultData[$i];
30+
$tmpKey = $i;
31+
for ($j = $i; $j < $this->dataLen; $j++) {
32+
if ($tmp > $this->resultData[$j]) {
33+
$tmp = $this->resultData[$j];
34+
$tmpKey = $j;
35+
}
36+
}
37+
38+
if ($tmpKey != $i) {
39+
$this->resultData[$tmpKey] = $this->resultData[$i];
40+
$this->resultData[$i] = $tmp;
41+
}
42+
}
43+
return $this->resultData;
44+
}
45+
}
46+
47+
$testData = [12, 55, 99, 22, 33, 25, 11, 44, 78];
48+
49+
$simpleSelectSort = new SimpleSelectSort($testData);
50+
echo '<pre>';
51+
var_dump($simpleSelectSort->sort());

0 commit comments

Comments
 (0)