-
Notifications
You must be signed in to change notification settings - Fork 5
/
BogoSort.php
56 lines (44 loc) · 1.34 KB
/
BogoSort.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
<?php
/*
An implementation of BogoSort on an array of integers.
https://en.wikipedia.org/wiki/Bogosort
This "sort" is basically the worst sort possible and amounts to random reordering
DO NOT USE THIS SORTING FUNCTION - IT IS A DEMONSTRATION OF WHAT >>>NOT<<< TO DO
*/
function BogoSort($array) {
/* If $array is set */
if(isset($array)) {
$array_length = count($array); /* get array length */
$num_of_shuffles = 0;
/* while $array is NOT in order */
while(!isInOrder($array, $array_length)) {
/* shuffle $array */
shuffle($array);
$num_of_shuffles++;
echo "Shuffle: " . $num_of_shuffles . '<br>' . PHP_EOL;
}
return $array;
}else {/* $array not set issue message and end program */
die("No Array! Use: BogoSort(ARRAY)");
}
}
function isInOrder($array, $array_length) {
for($i = 1; $i < $array_length; $i++) {
/* If the value to the left of this value is greator */
if($array[$i -1] > $array[$i]) {
return false;/* than return false because the list is not in order*/
}
}
return true; /* $array is in order */
}
set_time_limit ( 3600 ); // max run time 1 hour (adjust as needed)
$sorted = BogoSort(array(5,1,4,3,2,0));
var_dump($sorted);
echo '<br>' . PHP_EOL;
// Test proofs for provided array 0 - 5
for($i = 0; $i < 5; $i++){
if ($sorted[$i] == $i) {
echo "$i is correct<br>" . PHP_EOL;
}
}
?>