You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Bucket Sort/README.markdown
+60-8Lines changed: 60 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,16 @@
1
1
# Bucket Sort
2
2
3
-
## Definition
4
3
[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:
5
4
6
-
1) Distribute the elements into buckets or bin.
5
+
1) Distribute the elements into buckets or bins.
7
6
2) Sort each bucket individually.
8
7
3) Merge the buckets in order to produce a sort array as results.
9
8
10
-
A more complete definition could be
11
9
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).
14
11
15
-
## Performance
16
12
17
-
Performance for execution time:
13
+
The performance for execution time is:
18
14
19
15
| Case | Performance |
20
16
|:-------------: |:---------------:|
@@ -24,10 +20,13 @@ Performance for execution time:
24
20
25
21
Where **n** = #elements and **k** = #buckets
26
22
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
+
27
26
28
27
## Pseudocode
29
28
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:
31
30
32
31
function bucketSort(array, n) is
33
32
buckets ← new array of n empty lists
@@ -48,6 +47,59 @@ A [pseudocode](https://en.wikipedia.org/wiki/Bucket_sort#Pseudocode) of the algo
48
47
49
48

50
49
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`.
0 commit comments