note 41277 added to function.shuffle

From: Date: Mon, 05 Apr 2004 13:31:19 +0000
Subject: note 41277 added to function.shuffle
Groups: php.notes 
Request: Send a blank email to php-notes+get-67652@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.shuffle.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41277 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41277&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41277&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67652) next »