|
1 | 1 | # Bucket Sort
|
2 | 2 |
|
3 |
| -TODO: Implementation in progress |
| 3 | +## Definition |
| 4 | +[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 | + |
| 6 | +1) Distribute the elements into buckets or bin. |
| 7 | +2) Sort each bucket individually. |
| 8 | +3) Merge the buckets in order to produce a sort array as results. |
| 9 | + |
| 10 | +A more complete definition could be |
| 11 | + |
| 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) |
| 14 | + |
| 15 | + |
| 16 | +## Pseudocode |
| 17 | + |
| 18 | +A [pseudocode](https://en.wikipedia.org/wiki/Bucket_sort#Pseudocode) of the algorithm is as follows: |
| 19 | + |
| 20 | + function bucketSort(array, n) is |
| 21 | + buckets ← new array of n empty lists |
| 22 | + for i = 0 to (length(array)-1) do |
| 23 | + insert array[i] into buckets[msbits(array[i], k)] |
| 24 | + for i = 0 to n - 1 do |
| 25 | + nextSort(buckets[i]); |
| 26 | + return the concatenation of buckets[0], ...., buckets[n-1] |
| 27 | + |
| 28 | + |
| 29 | +## Graphically explained |
| 30 | + |
| 31 | +###Distribute elements in buckets |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +###Sorting inside every bucket and merging |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +##Swift implementation |
| 40 | + |
| 41 | +###Classes |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +###Code |
| 46 | + |
| 47 | +Bucket Sort implementation use the following functions, data structures and protocols: |
| 48 | + |
| 49 | +####Main function |
| 50 | + |
| 51 | +`bucketSort` is a generic function that can apply the algorithm to any element of type T, as long as T is Sortable. |
| 52 | + |
| 53 | + public func bucketSort<T:Sortable>(elements: [T], distributor: Distributor,sorter: Sorter, buckets: [Bucket<T>]) -> [T] { |
| 54 | + precondition(allPositiveNumbers(elements)) |
| 55 | + precondition(enoughSpaceInBuckets(buckets, elements: elements)) |
| 56 | + |
| 57 | + var bucketsCopy = buckets |
| 58 | + for elem in elements { |
| 59 | + distributor.distribute(elem, buckets: &bucketsCopy) |
| 60 | + } |
| 61 | + |
| 62 | + var results = [T]() |
| 63 | + |
| 64 | + for bucket in bucketsCopy { |
| 65 | + results += bucket.sort(sorter) |
| 66 | + } |
| 67 | + |
| 68 | + return results |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +####Bucket |
| 73 | + |
| 74 | + public struct Bucket<T:Sortable> { |
| 75 | + var elements: [T] |
| 76 | + let capacity: Int |
| 77 | + |
| 78 | + public init(capacity: Int) { |
| 79 | + self.capacity = capacity |
| 80 | + elements = [T]() |
| 81 | + } |
| 82 | + |
| 83 | + public mutating func add(item: T) { |
| 84 | + if (elements.count < capacity) { |
| 85 | + elements.append(item) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public func sort(algorithm: Sorter) -> [T] { |
| 90 | + return algorithm.sort(elements) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +####Sortable |
| 95 | + |
| 96 | + public protocol Sortable: IntConvertible, Comparable { |
| 97 | + } |
| 98 | + |
| 99 | +####IntConvertible |
| 100 | + |
| 101 | +The algorithm is designed to sort integers, so all the elements to be sorted should be mapped to an integer value. |
| 102 | + |
| 103 | + public protocol IntConvertible { |
| 104 | + func toInt() -> Int |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | +####Sorter |
| 109 | + |
| 110 | + public protocol Sorter { |
| 111 | + func sort<T:Sortable>(items: [T]) -> [T] |
| 112 | + } |
| 113 | + |
| 114 | +####Distributor |
| 115 | + |
| 116 | + public protocol Distributor { |
| 117 | + func distribute<T:Sortable>(element: T, inout buckets: [Bucket<T>]) |
| 118 | + } |
| 119 | + |
| 120 | + |
4 | 121 |
|
0 commit comments