Skip to content

Commit 0c2f57d

Browse files
Update: example step by step and descr changes
1 parent 5873dd5 commit 0c2f57d

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

Bucket Sort/README.markdown

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
# Bucket Sort
22

3-
## Definition
43
[Bucket Sort or Bin Sort](https://en.wikipedia.org/wiki/Bucket_sort) is a distributed sorting algorithm, which sort elements from an array by performing these steps:
54

6-
1) Distribute the elements into buckets or bin.
5+
1) Distribute the elements into buckets or bins.
76
2) Sort each bucket individually.
87
3) Merge the buckets in order to produce a sort array as results.
98

10-
A more complete definition could be
119

12-
>
13-
Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. Bucket sort is a generalization of pigeonhole sort. Bucket sort can be implemented with comparisons and therefore can also be considered a comparison sort algorithm. The computational complexity estimates involve the number of buckets. [1](https://en.wikipedia.org/wiki/Bucket_sort)
10+
See the algorithm in action [here](https://www.cs.usfca.edu/~galles/visualization/BucketSort.html) and [here](http://www.algostructure.com/sorting/bucketsort.php).
1411

15-
## Performance
1612

17-
Performance for execution time:
13+
The performance for execution time is:
1814

1915
| Case | Performance |
2016
|:-------------: |:---------------:|
@@ -24,10 +20,13 @@ Performance for execution time:
2420

2521
Where **n** = #elements and **k** = #buckets
2622

23+
On the **best case** the algorithm distributes the elements uniformily between buckets, a few elements are placed on each bucket and sorting the buckets is *O(1)*. Rearranging the elements is one more run through the initial list.
24+
On the **worst case** the elements are sent all to the same bucket, making the process takes *O(n^2)*.
25+
2726

2827
## Pseudocode
2928

30-
A [pseudocode](https://en.wikipedia.org/wiki/Bucket_sort#Pseudocode) of the algorithm is as follows:
29+
A [pseudocode](https://en.wikipedia.org/wiki/Bucket_sort#Pseudocode) of the algorithm can be as follows:
3130

3231
function bucketSort(array, n) is
3332
buckets ← new array of n empty lists
@@ -48,6 +47,59 @@ A [pseudocode](https://en.wikipedia.org/wiki/Bucket_sort#Pseudocode) of the algo
4847

4948
![sorting each bucket and merge](https://upload.wikimedia.org/wikipedia/commons/3/39/Bucket_sort_2.png)
5049

50+
##An example
51+
52+
### Input
53+
54+
Suppose we have the following list of elements: `[2, 56, 4, 77, 26, 98, 55]`.
55+
And we define 10 buckets will be used. To determine the capacity of each bucket we need to know the `maximum element value`, in this case `98`.
56+
57+
So the buckets are:
58+
* `bucket 1`: from 0 to 9
59+
* `bucket 2`: from 10 to 19
60+
* `bucket 3`: from 20 to 29
61+
* and so on.
62+
63+
### Distribution
64+
65+
Now we need to choose a distribution function.
66+
67+
`bucketNumber = ( elementValue / totalNumberOfBuckets) + 1`
68+
69+
Such that applying that function we distribute all the elements in the buckets.
70+
In our example it is like following:
71+
72+
1. Apply the distribution function to `2`. `bucketNumber = (2 / 10) + 1 = 1`
73+
2. Apply the distribution function to `56`. `bucketNumber = (56 / 10) + 1 = 6`
74+
3. Apply the distribution function to `4`. `bucketNumber = (4 / 10) + 1 = 1`
75+
4. Apply the distribution function to `77`. `bucketNumber = (77 / 10) + 1 = 8`
76+
5. Apply the distribution function to `26`. `bucketNumber = (26 / 10) + 1 = 3`
77+
6. Apply the distribution function to `98`. `bucketNumber = (98 / 10) + 1 = 10`
78+
7. Apply the distribution function to `55`. `bucketNumber = (55 / 10) + 1 = 6`
79+
80+
Our buckets will be filled now:
81+
82+
**1** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[2, 4]`
83+
**2** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[]`
84+
**3** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[26]`
85+
**4** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[]`
86+
**5** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[]`
87+
**6** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[55, 56]`
88+
**7** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[]`
89+
**8** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[77]`
90+
**9** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[]`
91+
**10** <img src="https://pixabay.com/static/uploads/photo/2014/03/24/17/21/pail-295491_960_720.png" width="40">: `[98]`
92+
93+
We can choose to insert the elements in every bucket in order, or sort every bucket after distributing all the elements.
94+
95+
### Put the elements back in the list
96+
97+
Finally we go through all the buckets and put the elements back in the list:
98+
99+
`[2, 4, 26, 55, 56, 77, 98]`
100+
101+
102+
51103
##Swift implementation
52104

53105
###Classes

0 commit comments

Comments
 (0)