Skip to content

Commit b580133

Browse files
committed
Starting work Swift 2.2 updates..
- includes changes to sorting, math, queue and stack algorithms.
1 parent d0f2aee commit b580133

File tree

9 files changed

+109
-71
lines changed

9 files changed

+109
-71
lines changed

Source/Factories/LinkedList.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class LinkedList<T: Equatable> {
3232
//cycle through the list of items
3333
while current.next != nil {
3434
current = current.next!
35-
x++
35+
x += 1
3636
}
3737

3838
return x
@@ -123,7 +123,7 @@ public class LinkedList<T: Equatable> {
123123
//cycle through the list of items
124124
while (index != x) {
125125
current = current.next
126-
x++
126+
x += 1
127127
}
128128

129129
return current
@@ -247,7 +247,7 @@ public class LinkedList<T: Equatable> {
247247
//update the assignments
248248
trailer = current
249249
current = current?.next
250-
listIndex++
250+
listIndex += 1
251251

252252
} //end while
253253

Source/Factories/Math.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,28 @@ class Math {
4444

4545

4646

47+
func someFunction(parameterWithDefault: Int = 12) {
48+
// function body goes here
49+
// if no arguments are passed to the function call,
50+
// value of parameterWithDefault is 12
51+
52+
someFunction(9)
53+
}
54+
55+
56+
57+
/*
58+
TODO: Recursive functions with parameters are still allowed in Swift. It's
59+
only the mutation of the variable within the method scope that isn't allowed.
60+
Just fix by providing a secondary "mutated" output variable..
61+
*/
62+
63+
4764
//build fibonacci sequence to a specified position - recursive
4865
func fib(n: Int, var sequence: Array<Int> = [0, 1]) {
4966

67+
someFunction()
68+
someFunction(6)
5069

5170
//initialize sequence
5271
if n < 2 {
@@ -68,9 +87,7 @@ class Math {
6887

6988
//set iteration
7089
fib(n, sequence: sequence)
71-
7290

73-
7491
}
7592

7693

Source/Factories/Queue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class Queue<T> {
3636
//cycle through the list of items
3737
while (current.next != nil) {
3838
current = current.next
39-
x++
39+
x += 1
4040
}
4141

4242
return x

Source/Factories/Sorting.swift

Lines changed: 75 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class Sorting {
9797

9898
func insertionSort(numberList: Array<Int>) -> Array<Int> {
9999

100-
100+
101101
//check for trivial case
102102
guard numberList.count > 1 else {
103103
return numberList
@@ -108,35 +108,39 @@ public class Sorting {
108108
var output = numberList
109109

110110

111-
for primaryIndex in 0..<output.count {
112-
113-
let key = output[primaryIndex]
111+
for primaryindex in 0..<output.count {
114112

113+
let key = output[primaryindex]
114+
var secondaryindex = primaryindex
115115

116-
for var secondaryIndex = primaryIndex; secondaryIndex > -1; secondaryIndex-- {
116+
while secondaryindex > -1 {
117117

118-
print("comparing \(key) and \(output[secondaryIndex])")
118+
print("comparing \(key) and \(output[secondaryindex])")
119119

120-
if key < output[secondaryIndex] {
120+
if key < output[secondaryindex] {
121121

122122
//move into correct position
123-
output.removeAtIndex(secondaryIndex + 1)
124-
output.insert(key, atIndex: secondaryIndex)
123+
output.removeAtIndex(secondaryindex + 1)
124+
output.insert(key, atIndex: secondaryindex)
125125

126126
}
127+
128+
secondaryindex -= 1
127129
}
130+
131+
128132
}
129133

130134

131135
return output
132136

133137
}
134-
138+
135139

136140

137141

138142
/*
139-
insertion sort algorithm - (Generics)
143+
insertion sort algorithm - generic
140144
*/
141145

142146
func insertionSortG<T: Comparable>(sequence: [T]) -> [T] {
@@ -150,24 +154,28 @@ public class Sorting {
150154
//mutated copy
151155
var output = Array(sequence)
152156

153-
154-
for primaryIndex in 0..<output.count {
157+
158+
for primaryindex in 0..<output.count {
155159

156-
let key = output[primaryIndex]
160+
let key = output[primaryindex]
161+
var secondaryindex = primaryindex
157162

158-
159-
for var secondaryIndex = primaryIndex; secondaryIndex > -1; secondaryIndex-- {
163+
while secondaryindex > -1 {
160164

161-
print("comparing \(key) and \(output[secondaryIndex])")
165+
print("comparing \(key) and \(output[secondaryindex])")
162166

163-
if key < output[secondaryIndex] {
164-
167+
if key < output[secondaryindex] {
168+
165169
//move into correct position
166-
output.removeAtIndex(secondaryIndex + 1)
167-
output.insert(key, atIndex: secondaryIndex)
170+
output.removeAtIndex(secondaryindex + 1)
171+
output.insert(key, atIndex: secondaryindex)
168172

169173
}
174+
175+
secondaryindex -= 1
170176
}
177+
178+
171179
}
172180

173181

@@ -229,7 +237,7 @@ public class Sorting {
229237

230238

231239
/*
232-
bubble sort algorithm - (Generics)
240+
bubble sort algorithm - generic
233241
*/
234242

235243
func bubbleSortG<T: Comparable>(sequence: [T]) -> [T] {
@@ -274,46 +282,48 @@ public class Sorting {
274282

275283
//MARK: - Selection Sort
276284

277-
278285
/*
279-
selection sort algorithm - rank items from the lowest to highest by iterating through
280-
the array and swapping the current iteration with the lowest value in the rest of the array
281-
until it reaches the end of the array.
282-
*/
283-
286+
selection sort algorithm - rank items from the lowest to highest by iterating through
287+
the array and swapping the current iteration with the lowest value in the rest of the array
288+
until it reaches the end of the array.
289+
*/
290+
284291
func selectionSort(numberList: Array<Int>) -> Array<Int> {
285292

286-
293+
287294
//check for trivial case
288295
guard numberList.count > 1 else {
289296
return numberList
290297
}
291-
298+
292299

293300
//mutated copy
294301
var output = numberList
295302

296303

297-
for primaryIndex in 0..<output.count {
304+
for primaryindex in 0..<output.count {
298305

299-
var minimum = primaryIndex
300306

301-
// iterate through remainder
302-
for var secondaryIndex = primaryIndex + 1; secondaryIndex < output.count; secondaryIndex++ {
303-
307+
var minimum = primaryindex
308+
var secondaryindex = primaryindex + 1
309+
310+
311+
while secondaryindex < output.count {
304312

305-
print("comparing \(output[minimum]) and \(output[secondaryIndex])")
313+
print("comparing \(output[minimum]) and \(output[secondaryindex])")
306314

307315
// store lowest value as minimum
308-
if output[minimum] > output[secondaryIndex] {
309-
minimum = secondaryIndex
316+
if output[minimum] > output[secondaryindex] {
317+
minimum = secondaryindex
310318
}
319+
320+
secondaryindex += 1
311321
}
312322

313323

314324
// swap minimum value with array iteration
315-
if primaryIndex != minimum {
316-
swap(&output[primaryIndex], &output[minimum])
325+
if primaryindex != minimum {
326+
swap(&output[primaryindex], &output[minimum])
317327
}
318328

319329
}
@@ -322,10 +332,11 @@ public class Sorting {
322332
return output
323333

324334
}
335+
325336

326337

327338
/*
328-
selection sort algorithm - (Generics)
339+
selection sort algorithm - generic
329340
*/
330341

331342
func selectionSortG<T: Comparable>(sequence: [T]) -> [T] {
@@ -340,26 +351,29 @@ public class Sorting {
340351
var output = Array(sequence)
341352

342353

343-
for primaryIndex in 0..<output.count {
354+
for primaryindex in 0..<output.count {
344355

345-
var minimum = primaryIndex
346356

347-
// iterate through remainder
348-
for var secondaryIndex = primaryIndex + 1; secondaryIndex < output.count; secondaryIndex++ {
349-
357+
var minimum = primaryindex
358+
var secondaryindex = primaryindex + 1
359+
360+
361+
while secondaryindex < output.count {
350362

351-
print("comparing \(output[minimum]) and \(output[secondaryIndex])")
363+
print("comparing \(output[minimum]) and \(output[secondaryindex])")
352364

353365
// store lowest value as minimum
354-
if output[minimum] > output[secondaryIndex] {
355-
minimum = secondaryIndex
366+
if output[minimum] > output[secondaryindex] {
367+
minimum = secondaryindex
356368
}
369+
370+
secondaryindex += 1
357371
}
358372

359373

360374
// swap minimum value with array iteration
361-
if primaryIndex != minimum {
362-
swap(&output[primaryIndex], &output[minimum])
375+
if primaryindex != minimum {
376+
swap(&output[primaryindex], &output[minimum])
363377
}
364378

365379
}
@@ -379,10 +393,17 @@ public class Sorting {
379393
// and moves values to the left or right of the pivot based on their value
380394
// it works recursively so that either side will be eventually sorted back to the top
381395

382-
func quickSort(var hops:[Int]) -> [Int] {
396+
func quickSort(sequence:[Int]) -> [Int] {
397+
398+
// immediately return the trivial cases
399+
guard sequence.count > 1 else {
400+
return sequence
401+
}
402+
403+
//mutated copy
404+
var hops = sequence
405+
383406

384-
guard hops.count > 1 else { return hops } // immediately return the trivial cases
385-
386407
let pivot = hops.removeAtIndex(0)
387408
var leftBucket:[Int] = []
388409
var rightBucket:[Int] = []

Source/Factories/Stack.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Stack<T> {
3232
//cycle through the list of items to get to the end.
3333
while ((current.next) != nil) {
3434
current = current.next!
35-
x++
35+
x += 1
3636
}
3737

3838
return x

SwiftStructures.xcodeproj/xcuserdata/waynebishop.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@
138138
ignoreCount = "0"
139139
continueAfterRunningActions = "No"
140140
filePath = "Source/Factories/Sorting.swift"
141-
timestampString = "479343308.067372"
141+
timestampString = "479344395.147185"
142142
startingColumnNumber = "9223372036854775807"
143143
endingColumnNumber = "9223372036854775807"
144-
startingLineNumber = "118"
145-
endingLineNumber = "118"
146-
landmarkName = "insertionSort(_:)"
144+
startingLineNumber = "29"
145+
endingLineNumber = "29"
146+
landmarkName = "binarySearch(_:key:)"
147147
landmarkType = "5">
148148
</BreakpointContent>
149149
</BreakpointProxy>
@@ -154,12 +154,12 @@
154154
ignoreCount = "0"
155155
continueAfterRunningActions = "No"
156156
filePath = "Source/Factories/Sorting.swift"
157-
timestampString = "479344395.147185"
157+
timestampString = "481680628.179238"
158158
startingColumnNumber = "9223372036854775807"
159159
endingColumnNumber = "9223372036854775807"
160-
startingLineNumber = "29"
161-
endingLineNumber = "29"
162-
landmarkName = "binarySearch(_:key:)"
160+
startingLineNumber = "135"
161+
endingLineNumber = "135"
162+
landmarkName = "insertionSort(_:)"
163163
landmarkType = "5">
164164
</BreakpointContent>
165165
</BreakpointProxy>

SwiftTests/MathTest.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class MathTest: XCTestCase {
3131
buildResultsTest(results)
3232

3333
}
34-
34+
35+
3536

3637
//recursive option
3738
func testFibRecursive() {

SwiftTests/SortingTest.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class SortingTest: XCTestCase {
8282

8383

8484
//MARK: General Sorting Algorithms
85-
8685

8786
func testInsertionSort() {
8887

0 commit comments

Comments
 (0)