May 2017
Intermediate to advanced
340 pages
8h 16m
English
The bucket sort is also known as bin sort. Bucket sort is a distribution sorting system where array elements are placed in different buckets. Each bucket is then sorted individually by either another sorting algorithm, or by applying recursive bucket sorting. An implementation of bucket sort using PHP can look like this:
function bucketSort(array &$data) { $n = count($data); if ($n <= 0) return; $min = min($data); $max = max($data); $bucket = []; $bLen = $max - $min + 1; $bucket = array_fill(0, $bLen, []); for ($i = 0; $i < $n; $i++) { array_push($bucket[$data[$i] - $min], $data[$i]); } $k = 0; for ($i = 0; $i < $bLen; $i++) { $bCount = count($bucket[$i]); for ($j = 0; $j < $bCount; $j++) { $data[$k] ...Read now
Unlock full access