function fillArray($depth, $max) { static $seed; if (is_null($seed)) { $seed = array('a', 2, 'c', 4, 'e', 6, 'g', 8, 'i', 10); } if ($depth < $max) { $node = array(); foreach ($seed as $key) { $node[$key] = fillArray($depth + 1, $max); } return $node; } return 'empty'; }
} } $nextSigFig = false; for ($k = 0; $k < $maxDigits; $k++) { for ($i = 0; $i < count($array); $i++) { if (!$nextSigFig) { $bucket[$array[$i] % 10][] = $array[$i]; } else { $bucket[floor($array[$i] / pow(10, $k)) % 10][] = $array[$i]; } } //Reset array and load back values from bucket. $array = array(); for ($j = 0; $j < count($bucket); $j++) { foreach ($bucket[$j] as $value) { $array[] = $value; } } //Reset bucket $bucket = array_fill(0, 9, array()); $nextSigFig = true; } return $array; } $time_start = microtime_float(); $array = radixSort(fillArray()); $time_end = microtime_float(); $duration = $time_end - $time_start; echo $duration; //Finishes sorting 10000 elements in ~0.68 seconds var_dump($array);