|
| 1 | +// |
| 2 | +// BucketSort.swift |
| 3 | +// |
| 4 | +// Created by Barbara Rodeker on 4/4/16. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 7 | +// associated documentation files (the "Software"), to deal in the Software without restriction, including |
| 8 | +// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the |
| 10 | +// following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all copies or substantial |
| 13 | +// portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT |
| 16 | +// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO |
| 17 | +// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 18 | +// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 19 | +// OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | +// |
| 21 | +// |
| 22 | + |
| 23 | +import Foundation |
| 24 | + |
| 25 | +////////////////////////////////////// |
| 26 | +// MARK: Main algorithm |
| 27 | +////////////////////////////////////// |
| 28 | + |
| 29 | + |
| 30 | +public func bucketSort<T:Sortable>(elements: [T], distributor: Distributor,sorter: Sorter, buckets: [Bucket<T>]) -> [T] { |
| 31 | + var bucketsCopy = buckets |
| 32 | + for elem in elements { |
| 33 | + distributor.distribute(elem, buckets: &bucketsCopy) |
| 34 | + } |
| 35 | + |
| 36 | + var results = [T]() |
| 37 | + |
| 38 | + for bucket in buckets { |
| 39 | + results += bucket.sort(sorter) |
| 40 | + } |
| 41 | + |
| 42 | + return results |
| 43 | +} |
| 44 | + |
| 45 | +////////////////////////////////////// |
| 46 | +// MARK: Distributor |
| 47 | +////////////////////////////////////// |
| 48 | + |
| 49 | + |
| 50 | +public protocol Distributor { |
| 51 | + func distribute<T:Sortable>(element: T, inout buckets: [Bucket<T>]) |
| 52 | +} |
| 53 | + |
| 54 | +/* |
| 55 | + * An example of a simple distribution function that send every elements to |
| 56 | + * the bucket representing the range in which it fits.An |
| 57 | + * |
| 58 | + * If the range of values to sort is 0..<49 i.e, there could be 5 buckets of capacity = 10 |
| 59 | + * So every element will be classified by the ranges: |
| 60 | + * |
| 61 | + * - 0 ..< 10 |
| 62 | + * - 10 ..< 20 |
| 63 | + * - 20 ..< 30 |
| 64 | + * - 30 ..< 40 |
| 65 | + * - 40 ..< 50 |
| 66 | + * |
| 67 | + * By following the formula: element / capacity = #ofBucket |
| 68 | + */ |
| 69 | +public struct RangeDistributor: Distributor { |
| 70 | + |
| 71 | + public init() {} |
| 72 | + |
| 73 | + public func distribute<T:Sortable>(element: T, inout buckets: [Bucket<T>]) { |
| 74 | + let value = element.toInt() |
| 75 | + let bucketCapacity = buckets.first!.capacity |
| 76 | + |
| 77 | + let bucketIndex = value / bucketCapacity |
| 78 | + buckets[bucketIndex].add(element) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +////////////////////////////////////// |
| 83 | +// MARK: Sortable |
| 84 | +////////////////////////////////////// |
| 85 | + |
| 86 | +public protocol IntConvertible { |
| 87 | + func toInt() -> Int |
| 88 | +} |
| 89 | + |
| 90 | +public protocol Sortable: IntConvertible, Comparable { |
| 91 | +} |
| 92 | + |
| 93 | +////////////////////////////////////// |
| 94 | +// MARK: Sorter |
| 95 | +////////////////////////////////////// |
| 96 | + |
| 97 | +public protocol Sorter { |
| 98 | + func sort<T:Sortable>(items: [T]) -> [T] |
| 99 | +} |
| 100 | + |
| 101 | +public struct InsertionSorter: Sorter { |
| 102 | + |
| 103 | + public init() {} |
| 104 | + |
| 105 | + public func sort<T:Sortable>(items: [T]) -> [T] { |
| 106 | + var results = items |
| 107 | + for i in 0 ..< results.count { |
| 108 | + var j = i |
| 109 | + while ( j > 0 && results[j-i] > results[j]) { |
| 110 | + |
| 111 | + let auxiliar = results[i] |
| 112 | + results[i] = results[j] |
| 113 | + results[j] = auxiliar |
| 114 | + |
| 115 | + j -= 1 |
| 116 | + } |
| 117 | + } |
| 118 | + return results |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +////////////////////////////////////// |
| 123 | +// MARK: Bucket |
| 124 | +////////////////////////////////////// |
| 125 | + |
| 126 | +public struct Bucket<T:Sortable> { |
| 127 | + var elements: [T] |
| 128 | + let capacity: Int |
| 129 | + |
| 130 | + public init(capacity: Int) { |
| 131 | + self.capacity = capacity |
| 132 | + elements = [T]() |
| 133 | + } |
| 134 | + |
| 135 | + public mutating func add(item: T) { |
| 136 | + if (elements.count < capacity) { |
| 137 | + elements.append(item) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public func sort(algorithm: Sorter) -> [T] { |
| 142 | + return algorithm.sort(elements) |
| 143 | + } |
| 144 | +} |
0 commit comments