note 41277 deleted from function.shuffle by vincent
| From: | vincent@php.net | Date: | Mon, 05 Apr 2004 15:41:25 +0000 |
| Subject: | note 41277 deleted from function.shuffle by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-67664@lists.php.net to get a copy of this message | ||
Note Submitter: fdellutri at tiscali dot it
----
dear chvickers,
thanks for idea, but your "mix" function works as an O(n^3) where n is the size of
$org_arr. Then, I've replaced this function with:
function mix($org_arr) {
$keys = array_keys($org_arr);
$max = count($org_arr);
$new_arr = array();
while (count($new_arr) < $max) {
$r = rand(0,count($keys)-1); // works better than rand
$new_arr[$keys[$r]] = $org_arr[$keys[$r]];
unset($keys[$r]);
$keys = array_values($keys);
}
return $new_arr;
}
That have a O(n) complexity.
Fabio