-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
136 lines (122 loc) · 2.86 KB
/
functions.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
function swap(&$a, &$b)
{
$a ^= $b;
$b ^= $a;
$a ^= $b;
}
//冒泡排序
function bubbleSort(&$arr)
{
$length = count($arr);
for ($i = $length - 1; $i >= 0; $i--) {
for ($j = 0; $j < $i; $j++) {
if ($arr[$j] < $arr[$j + 1]) {
swap($arr[$j], $arr[$j + 1]);
}
}
}
}
// 有序数组合并
function merge(&$arr, $low, $middle, $high)
{
$temp = [];
$i = $low;
$j = $middle + 1;
$k = $low;
while ($i <= $middle && $j <= $high) {
if ($arr[$i] < $arr[$j]) {
$temp[$k++] = $arr[$i++];
} else {
$temp[$k++] = $arr[$j++];
}
}
while ($i <= $middle) {
$temp[$k++] = $arr[$i++];
}
while ($j <= $high) {
$temp[$k++] = $arr[$j++];
}
foreach ($temp as $index => $value) {
$arr[$index] = $value;
}
}
// 合并排序
function mergeSort(&$arr, $low, $high)
{
if ($low < $high) {
$middle = intval(($low + $high) / 2);
mergeSort($arr, $low, $middle);
mergeSort($arr, $middle + 1, $high);
merge($arr, $low, $middle, $high);
}
}
/*$arr = [2, 9, 5, 1, 7, 3, 8];
mergeSort($arr, 0, count($arr) - 1);
print_r($arr);*/
// 快排
function quickSort(&$arr, $start, $end)
{
if ($start < $end) {
$left = $start;
$right = $end;
$middle = $arr[$left];
while ($left < $right) {
while ($left < $right && $middle < $arr[$right]) {
$right--;
}
$arr[$left] = $arr[$right];
while ($left < $right && $middle > $arr[$left]) {
$left++;
}
$arr[$right] = $arr[$left];
}
$arr[$left] = $middle;
quickSort($arr, $start, $left);
quickSort($arr, $left + 1, $end);
}
}
/*
$arr = [2, 9, 5, 1, 7, 3, 8];
quickSort($arr);
print_r($arr);
*/
// 全排
function fullArrange(&$arr, $index)
{
if ($index == 0) {
return [$arr[0]];
} else {
$rows = fullArrange($arr, $index - 1);
$temp_len = count($rows);
$temp_arr = [];
for ($k = 0; $k < $temp_len; $k++) {
$row = $rows[$k];
for ($j = 0; $j <= strlen($row); $j++) {
$str = substr($row, 0, $j) . $arr[$index] . substr($row, $j);
$temp_arr[] = $str;
}
}
return $temp_arr;
}
}
// 全组合
function fullCombine($arr, $index)
{
if ($index == 0) {
return [$arr[0]];
} else {
$rows = fullCombine($arr, $index - 1);
$temp_len = count($rows);
$rows[] = $arr[$index];
for ($k = 0; $k < $temp_len; $k++) {
$rows[] = $rows[$k] . $arr[$index];
}
return $rows;
}
}
//$arr = "abc";
//$arranges = fullArrange($arr, 2);
//$combines = fullCombine($arr, 2);
//print_r($arranges);
//print_r($combines);