}
    }
    $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);
Ejemplo n.º 2
0
            if ($number[$maxDigits - 1 - $i] == 0) {
                //put it in the 0 bucket
                array_push($buckets[0], $arr[$j]);
                //else it is a 1
            } else {
                //put it in the 1 bucket
                array_push($buckets[1], $arr[$j]);
            }
        }
        //resort into our array
        $k = 0;
        foreach ($buckets as $bucket) {
            foreach ($bucket as $value) {
                $arr[$k] = $value;
                $k++;
            }
        }
    }
    return $arr;
}
$arr = populateArray();
$time_start = microtime_float();
radixSort($arr);
$time_end = microtime_float();
echo "<h1>Regular Radix Sort</h1>";
echo $time_end - $time_start . "<br>";
$time_start = microtime_float();
binaryRadixSort($arr);
$time_end = microtime_float();
echo "<h1>Binary Radix Sort</h1>";
echo $time_end - $time_start;