8000 Migration of code to swift 3 style · codee/swift-algorithm-club@e9eeab8 · GitHub
[go: up one dir, main page]

Skip to content

Commit e9eeab8

Browse files
author
Divyendu Singh
committed
Migration of code to swift 3 style
This includes 1. Correct location for inout keyword 2. Correct function calls
1 parent f57b2f3 commit e9eeab8

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

Bucket Sort/BucketSort.playground/Contents.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
//
2121
//
2222

23-
24-
25-
2623
//////////////////////////////////////
2724
// MARK: Extensions
2825
//////////////////////////////////////
@@ -38,8 +35,8 @@ extension Int: IntConvertible, Sortable {
3835
//////////////////////////////////////
3936

4037
let input = [1, 2, 4, 6, 10]
41-
let buckets = [Bucket<Int>(capacity: 15), Bucket<Int>(capacity: 15), Bucket<Int>(capacity: 15)]
38+
var buckets = [Bucket<Int>(capacity: 15), Bucket<Int>(capacity: 15), Bucket<Int>(capacity: 15)]
4239

43-
let sortedElements = bucketSort(input, distributor: RangeDistributor(), sorter: InsertionSorter(), buckets: buckets)
40+
let sortedElements = bucketSort(elements: input, distributor: RangeDistributor(), sorter: InsertionSorter(), buckets: &buckets)
4441

4542
print(sortedElements)

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@ import Foundation
2727
//////////////////////////////////////
2828

2929

30-
public func bucketSort<T: Sortable>(elements: [T], distributor: Distributor, sorter: Sorter, buckets: [Bucket<T>]) -> [T] {
31-
var bucketsCopy = buckets
30+
public func bucketSort<T: Sortable>(elements: [T], distributor: Distributor, sorter: Sorter, buckets: inout [Bucket<T>]) -> [T] {
3231
for elem in elements {
33-
distributor.distribute(elem, buckets: &bucketsCopy)
32+
distributor.distribute(element: elem, buckets: &buckets)
3433
}
3534

3635
var results = [T]()
3736

3837
for bucket in buckets {
39-
results += bucket.sort(sorter)
38+
results += bucket.sort(algorithm: sorter)
4039
}
4140

4241
return results
@@ -48,7 +47,7 @@ public func bucketSort<T: Sortable>(elements: [T], distributor: Distributor, sor
4847

4948

5049
public protocol Distributor {
51-
func distribute<T: Sortable>(element: T, inout buckets: [Bucket<T>])
50+
func distribute<T: Sortable>(element: T, buckets: inout [Bucket<T>])
5251
}
5352

5453
/*
@@ -70,12 +69,12 @@ public struct RangeDistributor: Distributor {
7069

7170
public init() {}
7271

73-
public func distribute<T: Sortable>(element: T, inout buckets: [Bucket<T>]) {
72+
public func distribute<T: Sortable>(element: T, buckets: inout [Bucket<T>]) {
7473
let value = element.toInt()
7574
let bucketCapacity = buckets.first!.capacity
7675

7776
let bucketIndex = value / bucketCapacity
78-
buckets[bucketIndex].add(element)
77+
buckets[bucketIndex].add(item: element)
7978
}
8079
}
8180

@@ -139,6 +138,6 @@ public struct Bucket<T:Sortable> {
139138
}
140139

141140
public func sort(algorithm: Sorter) -> [T] {
142-
return algorithm.sort(elements)
141+
return algorithm.sort(items: elements)
143142
}
144143
}

0 commit comments

Comments
 (0)
0