Created
February 11, 2011 16:36
-
-
Save flevour/822603 to your computer and use it in GitHub Desktop.
A self-created exercise on bad algo for array ordering.
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 recursive_ordering($array) { | |
if (count($array) <= 1) { | |
return $array; | |
} | |
// Find minimum | |
$minumum_id = 0; | |
$minimum = $array[$minumum_id]; | |
foreach ($array as $i => $el) { | |
if ($el < $minimum) { | |
$minimum = $el; | |
$minumum_id = $i; | |
} | |
} | |
unset($array[$minumum_id]); | |
return array_merge(array($minimum), recursive_ordering($array)); | |
} | |
$result = recursive_ordering($array); | |
var_dump($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment