-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathradixSort.php
74 lines (54 loc) · 1.36 KB
/
radixSort.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* 基数排序
*/
require_once __DIR__ . '/../random.php';
function maxLength(array $data)
{
$maxLength = 0;
for ($i = 0; $i < count($data); $i++) {
$item = $data[$i];
$currentLength = count(str_split($item));
$maxLength = $currentLength > $maxLength ? $currentLength : $maxLength;
}
return $maxLength;
}
function getRadixByLoop(int $item, int $loop)
{
$arr = str_split($item);
if (count($arr) - 1 - $loop < 0) return 0;
return (int) $arr[count($arr) - 1 - $loop];
}
function doSort(&$arr, int $loopCount)
{
$bucket = array_fill(0, 10, []);
$data = [];
for ($i = 0; $i < count($arr); $i++) {
$item = $arr[$i];
$bucket[getRadixByLoop($item, $loopCount)][] = $item;
}
//还原数据
$k = 0;
for ($i = 0; $i < 10; $i++) {
$currentBucketLen = count($bucket[$i]);
for ($j = 0; $j < $currentBucketLen; $j++) {
$data[$k] = $bucket[$i][$j];
$k++;
}
}
$arr = $data;
}
function radixSort(&$data)
{
$maxLength = maxLength($data);
for ($loopCount = 0; $loopCount < $maxLength; $loopCount++) {
doSort($data, $loopCount);
}
}
$arr = randomArr(1, 1000, 100);
$start = microtime(true);
radixSort($arr);
$end = microtime(true);
$used = $end - $start;
echo "radixSort used $used s" . PHP_EOL;
var_dump($arr);