You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
541 B
PHTML
30 lines
541 B
PHTML
5 years ago
|
<?php
|
||
|
function issorted($list)
|
||
|
{
|
||
|
$cnt = count($list);
|
||
|
for($j = 1; $j < $cnt; $j++)
|
||
|
{
|
||
|
if($list[$j-1] > $list[$j])
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
function bogo_sort($list)
|
||
|
{
|
||
|
do
|
||
|
{
|
||
|
shuffle($list);
|
||
|
}
|
||
|
while(!issorted($list));
|
||
|
return $list;
|
||
|
}
|
||
|
$test_array = array(100, 0, 2, 5, -1, 4, 1);
|
||
|
echo "\nOriginal Array :\n";
|
||
|
echo implode(', ',$test_array );
|
||
|
echo "\nSorted Array :\n";
|
||
|
echo implode(', ',bogo_sort($test_array)). PHP_EOL;
|
||
|
|
||
|
?>
|