Skip to content

Commit cb43f85

Browse files
author
Osama Sayed
committed
Adding bucketsort
1 parent 03fff50 commit cb43f85

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

algorithms/sorting/bucketsort.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* Description: Bucket sort is a sorting algorithm that works by partitioning an array into a number of buckets.
5+
* Each bucket is then sorted separately by applying another sorting algorithm on it. Here I chose
6+
* to apply insertion sort algorithm.
7+
*
8+
* Complexity: worst is O(n * n), average O(n+k)
9+
* where k is the number of buckets
10+
*/
11+
12+
$inputArray = [5, 2, 6, 6, 1, 1, 3, 4, 12, 15, 23, 11, 8];
13+
bucketSort($inputArray);
14+
print_r($inputArray);
15+
16+
/**
17+
* Function to bucketSort an array of elements.
18+
*
19+
* @param array $inputArray the elements to be sorted.
20+
*/
21+
function bucketSort(&$inputArray)
22+
{
23+
$elementsCount = count($inputArray);
24+
$maximumNumber =$inputArray[0];
25+
$minimumNumber = $inputArray[0];
26+
$buckets = [];
27+
28+
// get the maximum and minimum elements in the input array
29+
for ($i = 1; $i < $elementsCount; $i++) {
30+
if ($inputArray[$i] < $minimumNumber) {
31+
$minimumNumber = $inputArray[$i];
32+
}
33+
if ($inputArray[$i] > $maximumNumber) {
34+
$maximumNumber = $inputArray[$i];
35+
}
36+
}
37+
38+
// create buckets of length $maximumNumber - $minimumNumber +1 and each bucket is an empty array for now
39+
for ($bucketIndex = 0; $bucketIndex < ($maximumNumber - $minimumNumber +1); $bucketIndex++) {
40+
$buckets[$bucketIndex] = [];
41+
}
42+
43+
// add each element in the input array to the array in the [$elementvalue - $minimumNumber] bucket
44+
for ($i = 0; $i < $elementsCount; $i++) {
45+
$buckets[$inputArray[$i] - $minimumNumber][] = $inputArray[$i];
46+
}
47+
48+
// now we go through each bucket and sort its elements using insertionSort
49+
foreach ($buckets as $bucket) {
50+
$count = count($bucket);
51+
if ($count && $count > 1) {
52+
insertionSort($bucket, count($bucket));
53+
}
54+
}
55+
56+
// we now put the elements back to the original array
57+
$inputArray = [];
58+
foreach ($buckets as $bucket) {
59+
foreach ($bucket as $bucketElement) {
60+
$inputArray[] = $bucketElement;
61+
}
62+
}
63+
}
64+
65+
/**
66+
* Function to apply an insertion sort on an array.
67+
*
68+
* @param array $array the array to be sorted
69+
* @param int $elementsNumber the amount of elements in the array
70+
*/
71+
function insertionSort(&$array, $elementsNumber)
72+
{
73+
// Iterate from the second element till the last one in the array.
74+
for ($currentElementIndex = 1; $currentElementIndex < $elementsNumber; $currentElementIndex++) {
75+
$value = $array[$currentElementIndex];
76+
$holeIndex = $currentElementIndex;
77+
// going backwards through the elements of the sorted array
78+
while ($holeIndex > 0 && $array[$holeIndex - 1] > $value) {
79+
$array[$holeIndex] = $array[$holeIndex - 1]; // shift the larger number towards the right.
80+
$holeIndex--;
81+
}
82+
$array[$holeIndex] = $value;
83+
}
84+
}

0 commit comments

Comments
 (0)