Skip to content

Commit 7d1c4a3

Browse files
committed
Update Insertion Sort to Swift 3
1 parent 9ecfd88 commit 7d1c4a3

File tree

6 files changed

+33
-25
lines changed

6 files changed

+33
-25
lines changed

Diff for: Insertion Sort/InsertionSort.playground/Contents.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//: Playground - noun: a place where people can play
22

3-
func insertionSort<T>(array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
3+
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
44
var a = array
55
for x in 1..<a.count {
66
var y = x
@@ -16,4 +16,4 @@ func insertionSort<T>(array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
1616

1717
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
1818
insertionSort(list, <)
19-
insertionSort(list, >)
19+
insertionSort(list, >)

Diff for: Insertion Sort/InsertionSort.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func insertionSort<T>(array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
1+
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
22
guard array.count > 1 else { return array }
33

44
var a = array

Diff for: Insertion Sort/README.markdown

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ This was a description of the inner loop of the insertion sort algorithm, which
9090
Here is an implementation of insertion sort in Swift:
9191

9292
```swift
93-
func insertionSort(array: [Int]) -> [Int] {
93+
func insertionSort(_ array: [Int]) -> [Int] {
9494
var a = array // 1
9595
for x in 1..<a.count { // 2
9696
var y = x
@@ -151,7 +151,7 @@ Instead of swapping with each of the previous elements, we can just shift all th
151151
In code that looks like this:
152152

153153
```swift
154-
func insertionSort(array: [Int]) -> [Int] {
154+
func insertionSort(_ array: [Int]) -> [Int] {
155155
var a = array
156156
for x in 1..<a.count {
157157
var y = x
@@ -175,7 +175,7 @@ It would be nice to sort other things than just numbers. We can make the datatyp
175175
The function signature becomes:
176176

177177
```swift
178-
func insertionSort<T>(array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
178+
func insertionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
179179
```
180180

181181
The array has type `[T]` where `T` is the placeholder type for the generics. Now `insertionSort()` will accept any kind of array, whether it contains numbers, strings, or something else.

Diff for: Insertion Sort/Tests/Tests.xcodeproj/project.pbxproj

+9-1
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@
8686
isa = PBXProject;
8787
attributes = {
8888
LastSwiftUpdateCheck = 0720;
89-
LastUpgradeCheck = 0720;
89+
LastUpgradeCheck = 0800;
9090
ORGANIZATIONNAME = "Swift Algorithm Club";
9191
TargetAttributes = {
9292
7B2BBC7F1C779D720067B71D = {
9393
CreatedOnToolsVersion = 7.2;
94+
LastSwiftMigration = 0800;
9495
};
9596
};
9697
};
@@ -149,8 +150,10 @@
149150
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
150151
CLANG_WARN_EMPTY_BODY = YES;
151152
CLANG_WARN_ENUM_CONVERSION = YES;
153+
CLANG_WARN_INFINITE_RECURSION = YES;
152154
CLANG_WARN_INT_CONVERSION = YES;
153155
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
156+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
154157
CLANG_WARN_UNREACHABLE_CODE = YES;
155158
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
156159
CODE_SIGN_IDENTITY = "-";
@@ -193,8 +196,10 @@
193196
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
194197
CLANG_WARN_EMPTY_BODY = YES;
195198
CLANG_WARN_ENUM_CONVERSION = YES;
199+
CLANG_WARN_INFINITE_RECURSION = YES;
196200
CLANG_WARN_INT_CONVERSION = YES;
197201
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
202+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
198203
CLANG_WARN_UNREACHABLE_CODE = YES;
199204
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
200205
CODE_SIGN_IDENTITY = "-";
@@ -213,6 +218,7 @@
213218
MACOSX_DEPLOYMENT_TARGET = 10.11;
214219
MTL_ENABLE_DEBUG_INFO = NO;
215220
SDKROOT = macosx;
221+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
216222
};
217223
name = Release;
218224
};
@@ -224,6 +230,7 @@
224230
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
225231
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
226232
PRODUCT_NAME = "$(TARGET_NAME)";
233+
SWIFT_VERSION = 3.0;
227234
};
228235
name = Debug;
229236
};
@@ -235,6 +242,7 @@
235242
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
236243
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
237244
PRODUCT_NAME = "$(TARGET_NAME)";
245+
SWIFT_VERSION = 3.0;
238246
};
239247
name = Release;
240248
};

Diff for: Insertion Sort/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Diff for: Quicksort/Tests/SortingTestHelpers.swift

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import Foundation
22
import XCTest
33

4-
func randomArray(size: Int) -> [Int] {
4+
func randomArray(_ size: Int) -> [Int] {
55
var a = [Int]()
66
for _ in 1...size {
77
a.append(Int(arc4random_uniform(1000)))
88
}
99
return a
1010
}
1111

12-
func arrayIsSortedLowToHigh(a: [Int]) -> Bool {
12+
func arrayIsSortedLowToHigh(_ a: [Int]) -> Bool {
1313
for x in 1..<a.count {
1414
if a[x - 1] > a[x] { return false }
1515
}
1616
return true
1717
}
1818

19-
typealias SortFunction = [Int] -> [Int]
19+
typealias SortFunction = ([Int]) -> [Int]
2020

21-
func checkSortingRandomArray(sortFunction: SortFunction) {
21+
func checkSortingRandomArray(_ sortFunction: SortFunction) {
2222
let numberOfIterations = 100
2323
for _ in 1...numberOfIterations {
2424
let a = randomArray(Int(arc4random_uniform(100)) + 1)
@@ -28,73 +28,73 @@ func checkSortingRandomArray(sortFunction: SortFunction) {
2828
}
2929
}
3030

31-
func checkSortingEmptyArray(sortFunction: SortFunction) {
31+
func checkSortingEmptyArray(_ sortFunction: SortFunction) {
3232
let a = [Int]()
3333
let s = sortFunction(a)
3434
XCTAssertEqual(s.count, 0)
3535
}
3636

37-
func checkSortingArrayOneElement(sortFunction: SortFunction) {
37+
func checkSortingArrayOneElement(_ sortFunction: SortFunction) {
3838
let a = [123]
3939
let s = sortFunction(a)
4040
XCTAssertEqual(s, [123])
4141
}
4242

43-
func checkSortingArrayTwoElementsInOrder(sortFunction: SortFunction) {
43+
func checkSortingArrayTwoElementsInOrder(_ sortFunction: SortFunction) {
4444
let a = [123, 456]
4545
let s = sortFunction(a)
4646
XCTAssertEqual(s, [123, 456])
4747
}
4848

49-
func checkSortingArrayTwoElementsOutOfOrder(sortFunction: SortFunction) {
49+
func checkSortingArrayTwoElementsOutOfOrder(_ sortFunction: SortFunction) {
5050
let a = [456, 123]
5151
let s = sortFunction(a)
5252
XCTAssertEqual(s, [123, 456])
5353
}
5454

55-
func checkSortingArrayTwoEqualElements(sortFunction: SortFunction) {
55+
func checkSortingArrayTwoEqualElements(_ sortFunction: SortFunction) {
5656
let a = [123, 123]
5757
let s = sortFunction(a)
5858
XCTAssertEqual(s, [123, 123])
5959
}
6060

61-
func checkSortingArrayThreeElementsABC(sortFunction: SortFunction) {
61+
func checkSortingArrayThreeElementsABC(_ sortFunction: SortFunction) {
6262
let a = [2, 4, 6]
6363
let s = sortFunction(a)
6464
XCTAssertEqual(s, [2, 4, 6])
6565
}
6666

67-
func checkSortingArrayThreeElementsACB(sortFunction: SortFunction) {
67+
func checkSortingArrayThreeElementsACB(_ sortFunction: SortFunction) {
6868
let a = [2, 6, 4]
6969
let s = sortFunction(a)
7070
XCTAssertEqual(s, [2, 4, 6])
7171
}
7272

73-
func checkSortingArrayThreeElementsBAC(sortFunction: SortFunction) {
73+
func checkSortingArrayThreeElementsBAC(_ sortFunction: SortFunction) {
7474
let a = [4, 2, 6]
7575
let s = sortFunction(a)
7676
XCTAssertEqual(s, [2, 4, 6])
7777
}
7878

79-
func checkSortingArrayThreeElementsBCA(sortFunction: SortFunction) {
79+
func checkSortingArrayThreeElementsBCA(_ sortFunction: SortFunction) {
8080
let a = [4, 6, 2]
8181
let s = sortFunction(a)
8282
XCTAssertEqual(s, [2, 4, 6])
8383
}
8484

85-
func checkSortingArrayThreeElementsCAB(sortFunction: SortFunction) {
85+
func checkSortingArrayThreeElementsCAB(_ sortFunction: SortFunction) {
8686
let a = [6, 2, 4]
8787
let s = sortFunction(a)
8888
XCTAssertEqual(s, [2, 4, 6])
8989
}
9090

91-
func checkSortingArrayThreeElementsCBA(sortFunction: SortFunction) {
91+
func checkSortingArrayThreeElementsCBA(_ sortFunction: SortFunction) {
9292
let a = [6, 4, 2]
9393
let s = sortFunction(a)
9494
XCTAssertEqual(s, [2, 4, 6])
9595
}
9696

97-
func checkSortAlgorithm(sortFunction: SortFunction) {
97+
func checkSortAlgorithm(_ sortFunction: SortFunction) {
9898
checkSortingEmptyArray(sortFunction)
9999
checkSortingArrayOneElement(sortFunction)
100100
checkSortingArrayTwoElementsInOrder(sortFunction)
@@ -109,6 +109,6 @@ func checkSortAlgorithm(sortFunction: SortFunction) {
109109
checkSortingRandomArray(sortFunction)
110110
}
111111

112-
func checkSortAlgorithm(sortFunction: ([Int], (Int, Int) -> Bool) -> [Int]) {
112+
func checkSortAlgorithm(_ sortFunction: @escaping ([Int], (Int, Int) -> Bool) -> [Int]) {
113113
checkSortAlgorithm { a in sortFunction(a, <) }
114114
}

0 commit comments

Comments
 (0)