Skip to content

Commit 5873dd5

Browse files
Adding time performance & alternatives.
1 parent 1e77724 commit 5873dd5

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Bucket Sort/README.markdown

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ A more complete definition could be
1212
>
1313
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)
1414

15+
## Performance
16+
17+
Performance for execution time:
18+
19+
| Case | Performance |
20+
|:-------------: |:---------------:|
21+
| Worst | O(n^2) |
22+
| Best | Omega(n + k) |
23+
| Average | Theta(n + k) |
24+
25+
Where **n** = #elements and **k** = #buckets
26+
1527

1628
## Pseudocode
1729

@@ -117,5 +129,73 @@ The algorithm is designed to sort integers, so all the elements to be sorted sho
117129
func distribute<T:Sortable>(element: T, inout buckets: [Bucket<T>])
118130
}
119131

132+
### Custom Sorter and Distributor
133+
134+
The current implementation make use of the following implementations for *Sorter* and *Distributor*.
135+
136+
*Sorter*
137+
138+
public struct InsertionSorter: Sorter {
139+
140+
public init() {}
141+
142+
public func sort<T:Sortable>(items: [T]) -> [T] {
143+
var results = items
144+
for i in 0 ..< results.count {
145+
var j = i
146+
while ( j > 0 && results[j-1] > results[j]) {
147+
148+
let auxiliar = results[j-1]
149+
results[j-1] = results[j]
150+
results[j] = auxiliar
151+
152+
j -= 1
153+
}
154+
}
155+
return results
156+
}
157+
}
158+
159+
*Distributor*
160+
161+
/*
162+
* An example of a simple distribution function that send every elements to
163+
* the bucket representing the range in which it fits.An
164+
*
165+
* If the range of values to sort is 0..<49 i.e, there could be 5 buckets of capacity = 10
166+
* So every element will be classified by the ranges:
167+
*
168+
* - 0 ..< 10
169+
* - 10 ..< 20
170+
* - 20 ..< 30
171+
* - 30 ..< 40
172+
* - 40 ..< 50
173+
*
174+
* By following the formula: element / capacity = #ofBucket
175+
*/
176+
public struct RangeDistributor: Distributor {
177+
178+
public init() {}
179+
180+
public func distribute<T:Sortable>(element: T, inout buckets: [Bucket<T>]) {
181+
let value = element.toInt()
182+
let bucketCapacity = buckets.first!.capacity
183+
184+
let bucketIndex = value / bucketCapacity
185+
buckets[bucketIndex].add(element)
186+
}
187+
}
188+
189+
### Make your own version
190+
191+
By reusing this code and implementing your own *Sorter* and *Distributor* you can experiment with different versions.
192+
193+
## Other variations of Bucket Sort
194+
195+
The following are some of the variation to the General Bucket Sort implemented here:
120196

197+
- [Proxmap Sort](https://en.wikipedia.org/wiki/Bucket_sort#ProxmapSort)
198+
- [Histogram Sort](https://en.wikipedia.org/wiki/Bucket_sort#Histogram_sort)
199+
- [Postman Sort](https://en.wikipedia.org/wiki/Bucket_sort#Postman.27s_sort)
200+
- [Shuffle Sort](https://en.wikipedia.org/wiki/Bucket_sort#Shuffle_sort)
121201

0 commit comments

Comments
 (0)