Skip to content

Commit 5822fbc

Browse files
committed
Updated Bucket Sort to Swift 3.0
1 parent d55a224 commit 5822fbc

File tree

1 file changed

+58
-58
lines changed

1 file changed

+58
-58
lines changed

Bucket Sort/BucketSort.playground/Sources/BucketSort.swift

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ import Foundation
2828

2929

3030
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
31+
var bucketsCopy = buckets
32+
for elem in elements {
33+
distributor.distribute(element: elem, buckets: &bucketsCopy)
34+
}
35+
36+
var results = [T]()
37+
38+
for bucket in buckets {
39+
results += bucket.sort(algorithm: sorter)
40+
}
41+
42+
return results
4343
}
4444

4545
//////////////////////////////////////
@@ -48,7 +48,7 @@ public func bucketSort<T: Sortable>(elements: [T], distributor: Distributor, sor
4848

4949

5050
public protocol Distributor {
51-
func distribute<T: Sortable>(element: T, inout buckets: [Bucket<T>])
51+
func distribute<T: Sortable>(element: T, buckets: inout [Bucket<T>])
5252
}
5353

5454
/*
@@ -67,24 +67,24 @@ public protocol Distributor {
6767
* By following the formula: element / capacity = #ofBucket
6868
*/
6969
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-
}
70+
71+
public init() {}
72+
73+
public func distribute<T: Sortable>(element: T, buckets: inout [Bucket<T>]) {
74+
let value = element.toInt()
75+
let bucketCapacity = buckets.first!.capacity
76+
77+
let bucketIndex = value / bucketCapacity
78+
buckets[bucketIndex].add(item: element)
79+
}
8080
}
8181

8282
//////////////////////////////////////
8383
// MARK: Sortable
8484
//////////////////////////////////////
8585

8686
public protocol IntConvertible {
87-
func toInt() -> Int
87+
func toInt() -> Int
8888
}
8989

9090
public protocol Sortable: IntConvertible, Comparable {
@@ -95,50 +95,50 @@ public protocol Sortable: IntConvertible, Comparable {
9595
//////////////////////////////////////
9696

9797
public protocol Sorter {
98-
func sort<T: Sortable>(items: [T]) -> [T]
98+
func sort<T: Sortable>(items: [T]) -> [T]
9999
}
100100

101101
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-
}
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
117119
}
118-
return results
119-
}
120120
}
121121

122122
//////////////////////////////////////
123123
// MARK: Bucket
124124
//////////////////////////////////////
125125

126126
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)
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(items: elements)
138143
}
139-
}
140-
141-
public func sort(algorithm: Sorter) -> [T] {
142-
return algorithm.sort(elements)
143-
}
144144
}

0 commit comments

Comments
 (0)