-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsorting.php
More file actions
251 lines (218 loc) · 4.2 KB
/
sorting.php
File metadata and controls
251 lines (218 loc) · 4.2 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* LightPHP Framework
* LitePHP is a framework that has been designed to be lite waight, extensible and fast.
*
* !! WARNING: This file is not yet been tested and is not expected to work !!
*
* @author Robert Pitt <robertpitt1988@gmail.com>
* @category core
* @copyright 2013 Robert Pitt
* @license GPL v3 - GNU Public License v3
* @version 1.0.0
*/
class Sorting_Library
{
/**
* Sort an array using the natural algorithm
*/
public function natural(array &$stack)
{
/**
* Use PHP's inbuilt function for this
*/
natsort($stack);
}
/**
* Sort an array using the bubble algorithm
*/
public function bubble(array &$stack)
{
$c = count($stack);
/**
* Start loopping the stack
*/
for($i = 0; $i < $c; $i++)
{
for ($j = 0, $stop = ($c - $i); $j < $stop; $j++)
{
if ($stack[$j] > $stack[j+1])
{
$this->_swap($stack, $j, $j + 1);
}
}
}
}
/**
* Sort an array using the insertion algorithm
*/
public function insertion(array &$stack)
{
$c = count($stack);
for ($i = 0; $i < $c; $i++)
{
/**
* store the current value because it may shift later
* @var *
*/
$value = $stack[$i];
/**
* Whenever the value in the sorted section is greater than the value
* in the unsorted section, shift all items in the sorted section over
* by one. This creates space in which to insert the value.
*/
for ($j = i - 1; $j > -1 && $stack[$j] > $value; $j--)
{
$stack[$j + 1] = $stack[$j];
}
$stack[$j + 1] = $value;
}
}
/**
* Sort an array using the merge algorithm
*/
public function merge(array &$stack)
{
$c = count($stack);
/**
* Return if we only have one item
*/
if($c === 1)
{
return $stack;
}
/**
* Declare a working stack
*/
$work = array();
for ($i = 0; $i < $c; $i++)
{
$work[] = [$stack[$i]];
}
$work[] = []; //In case we have an odd set.
/**
* Start looping the working set.
*/
for ($lim = $c; $lim > 1; $lim = floor(($lim + 1) / 2))
{
for ($j = 0, $k=0; $k < $lim; $j++, $k += 2)
{
$work[$j] = $this->_merge($work[$k], $work[$k + 1]);
}
$work[$j] = []; //In case we have an odd set.
}
/**
* Set the new stack to the working stack.
*/
$stack = $work[0];
}
/**
* Sort an array using the selection algorithm
*/
public function selection(array &$stack)
{
$c = count($stack);
for ($i = 0; $i < $c; $i++)
{
// set minimum to this position
$min = $i;
// check the rest of the array to see if anything is smaller
for ($j = $i + 1; $j < $c; $j++)
{
if($stack[$j] < $stack[$min])
{
$min = $j;
}
}
// if the minimum isn't in the position, swap it
if($i != $min)
{
$this->_swap($stack, $i, $min);
}
}
}
/**
* Shell sort
*/
public function shell(array &$stack)
{
$c = count($stack);
$g = floor($c / 2);
while($g > 0)
{
for($i = $g; $i < $c; $i++)
{
$temp = $stack[$i];
$j = $i;
while($j >= $g && $stack[$j - $g] > $temp)
{
$stack[$j] = $stack[$j - $g];
$j -= $g;
}
$stack[$j] = $temp;
}
$g = floor($g / 2);
}
}
/**
* Radix sorting
* @todo Implement.
*/
public function radix(&$input)
{
}
/**
* Swap the value of the first index with the value of the second index.
*/
private function _swap(array &$stack, $key_from, $key_to)
{
/**
* Get the key from the first value
*/
$temp = $stack[$key_from];
/**
* Replace the first index with the value of the second index
*/
$stack[$key_from] = $stack[$key_to];
/**
* Set the first value to the second index.
*/
$stack[$key_to] = $temp;
}
/**
* Merges to arrays in order based on their natural
* relationship.
*/
private function _merge($left, $right)
{
$result = array();
/**
* Get the lengths
*/
$lc = count($left);
$rc = count($right);
while ($lc > 0 && $rc > 0)
{
if ($left[0] < $right[0])
{
$result[] = array_shift($left);
$lc--;
}
else
{
$result[] = array_shift($right);
$rc--;
}
}
$result = array_merge($result, $left, $right);
/**
* Clear out the arrays
*/
unset($left);
unset($right);
/**
* Return the computed result
*/
return $result;
}
}