Skip to content

Commit 1101993

Browse files
authored
Merge branch 'master' into DepthFirstSearch-Swift4
2 parents 08cf693 + 721ef3c commit 1101993

File tree

25 files changed

+221
-162
lines changed

25 files changed

+221
-162
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: objective-c
2-
osx_image: xcode8.3
2+
osx_image: xcode9
33
# sudo: false
44

55
install:

Array2D/Array2D.playground/Contents.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// last checked with Xcode 9.0b4
2+
#if swift(>=4.0)
3+
print("Hello, Swift 4!")
4+
#endif
5+
16
/*
27
Two-dimensional array with a fixed number of rows and columns.
38
This is mostly handy for games that are played on a grid, such as chess.

Breadth-First Search/BreadthFirstSearch.playground/Pages/Simple example.xcplaygroundpage/Contents.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// last checked with Xcode 9.0b4
2+
#if swift(>=4.0)
3+
print("Hello, Swift 4!")
4+
#endif
5+
16
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
27
var queue = Queue<Node>()
38
queue.enqueue(source)

Depth-First Search/DepthFirstSearch.playground/Pages/Simple Example.xcplaygroundpage/Contents.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// last checked with Xcode 9.0b4
32
#if swift(>=4.0)
43
print("Hello, Swift 4!")

Linear Search/LinearSearch.playground/Contents.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
//: Playground - noun: a place where people can play
22

3+
// last checked with Xcode 4.0b4
4+
#if swift(>=4.0)
5+
print("Hello, Swift 4!")
6+
#endif
7+
38
func linearSearch<T: Equatable>(_ array: [T], _ object: T) -> Int? {
4-
for (index, obj) in array.enumerated() where obj == object {
5-
return index
6-
}
7-
return nil
9+
for (index, obj) in array.enumerated() where obj == object {
10+
return index
11+
}
12+
return nil
813
}
914

1015
let array = [5, 2, 4, 7]

Linear Search/LinearSearch.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

Selection Sort/SelectionSort.playground/Contents.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
//: Playground - noun: a place where people can play
22

3-
func selectionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
4-
guard array.count > 1 else { return array }
5-
var a = array
6-
for x in 0 ..< a.count - 1 {
7-
var lowest = x
8-
for y in x + 1 ..< a.count {
9-
if isOrderedBefore(a[y], a[lowest]) {
10-
lowest = y
11-
}
12-
}
13-
if x != lowest {
14-
swap(&a[x], &a[lowest])
15-
}
16-
}
17-
return a
18-
}
3+
// last checked with Xcode 9.0b4
4+
#if swift(>=4.0)
5+
print("Hello, Swift 4!")
6+
#endif
197

208
let list = [ 10, -1, 3, 9, 2, 27, 8, 5, 1, 3, 0, 26 ]
219
selectionSort(list, <)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public func selectionSort<T: Comparable>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
2+
guard array.count > 1 else { return array }
3+
4+
var a = array
5+
for x in 0 ..< a.count - 1 {
6+
7+
// Find the lowest value in the rest of the array.
8+
var lowest = x
9+
for y in x + 1 ..< a.count {
10+
if isOrderedBefore(a[y], a[lowest]) {
11+
lowest = y
12+
}
13+
}
14+
15+
// Swap the lowest value with the current array index.
16+
if x != lowest {
17+
a.swapAt(x, lowest)
18+
}
19+
}
20+
return a
21+
}

Selection Sort/SelectionSort.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

Selection Sort/SelectionSort.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
func selectionSort<T>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
2-
guard array.count > 1 else { return array }
1+
public func selectionSort<T: Comparable>(_ array: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
2+
guard array.count > 1 else { return array }
33

4-
var a = array
5-
for x in 0 ..< a.count - 1 {
4+
var a = array
5+
for x in 0 ..< a.count - 1 {
66

7-
// Find the lowest value in the rest of the array.
8-
var lowest = x
9-
for y in x + 1 ..< a.count {
10-
if isOrderedBefore(a[y], a[lowest]) {
11-
lowest = y
12-
}
13-
}
7+
// Find the lowest value in the rest of the array.
8+
var lowest = x
9+
for y in x + 1 ..< a.count {
10+
if isOrderedBefore(a[y], a[lowest]) {
11+
lowest = y
12+
}
13+
}
1414

15-
// Swap the lowest value with the current array index.
16-
if x != lowest {
17-
swap(&a[x], &a[lowest])
15+
// Swap the lowest value with the current array index.
16+
if x != lowest {
17+
a.swapAt(x, lowest)
18+
}
1819
}
19-
}
20-
return a
20+
return a
2121
}

0 commit comments

Comments
 (0)