Created
February 11, 2011 20:56
-
-
Save flevour/823014 to your computer and use it in GitHub Desktop.
Making a worse ordering algo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$array = array(5, 3, 2, 1, 7); | |
function worse_recursive_ordering($array) { | |
if (count($array) <= 1) { | |
return $array; | |
} | |
// Find minimum | |
$minimum = $array[0]; | |
foreach ($array as $i => $el) { | |
if ($el < $minimum) { | |
$minimum = $el; | |
} | |
} | |
// The weird ordering happens here | |
while ($array[0] != $minimum) shuffle($array); | |
return array_merge(array($minimum), worse_recursive_ordering(array_slice($array, 1))); | |
} | |
$result = worse_recursive_ordering($array); | |
var_dump($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment