Skip to content

Commit

Permalink
Completed Swift 2.2 updates..
Browse files Browse the repository at this point in the history
- updates to sorting algorithms
- updates to math related sequences
- minor update to graph algorithm helper function
  • Loading branch information
waynewbishop committed Apr 7, 2016
1 parent b580133 commit a362c9b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 46 deletions.
16 changes: 10 additions & 6 deletions Source/Factories/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ public class SwiftGraph {
/* reverse the sequence of paths given the shortest path.
process analagous to reversing a linked list. */

func reversePath(var head: Path!, source: Vertex) -> Path! {
func reversePath(head: Path!, source: Vertex) -> Path! {

if head == nil {
return nil

guard head != nil else {
return head
}

//mutated copy
var output = head


var current: Path! = head
var current: Path! = output
var prev: Path!
var next: Path!

Expand All @@ -112,10 +116,10 @@ public class SwiftGraph {
sourcePath.previous = prev
sourcePath.total = nil

head = sourcePath
output = sourcePath


return head
return output

}

Expand Down
46 changes: 17 additions & 29 deletions Source/Factories/Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ class Math {
func fib(n: Int) -> Array<Int>! {


if n < 2 {
//check trivial condition
guard n > 2 else {
return nil
}


//initialize the sequence
var sequence: Array<Int> = [0, 1]

Expand All @@ -41,52 +43,36 @@ class Math {
return sequence

}



func someFunction(parameterWithDefault: Int = 12) {
// function body goes here
// if no arguments are passed to the function call,
// value of parameterWithDefault is 12

someFunction(9)
}



/*
TODO: Recursive functions with parameters are still allowed in Swift. It's
only the mutation of the variable within the method scope that isn't allowed.
Just fix by providing a secondary "mutated" output variable..
*/


//build fibonacci sequence to a specified position - recursive
func fib(n: Int, var sequence: Array<Int> = [0, 1]) {

someFunction()
someFunction(6)
func fib(n: Int, sequence: Array<Int> = [0, 1]) {

//initialize sequence
if n < 2 {

//check trivial condition
guard n > 2 else {
return
}


//mutated copy
var output = sequence

let i: Int = sequence.count
let i: Int = output.count


//set base condition
if i == n {
return
}

let results: Int = sequence[i - 1] + sequence[i - 2]
sequence.append(results)
let results: Int = output[i - 1] + output[i - 2]
output.append(results)


//set iteration
fib(n, sequence: sequence)
fib(n, sequence: output)

}

Expand All @@ -95,7 +81,9 @@ class Math {
//build fibonacci sequence to a specified position - trailing closure
func fib(n: Int, formula: Array<Int> -> Int) -> Array<Int>! {

if n < 2 {

//check trivial condition
guard n > 2 else {
return nil
}

Expand Down
26 changes: 21 additions & 5 deletions Source/Factories/Sorting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ public class Sorting {

for primaryindex in 0..<output.count {


let key = output[primaryindex]

var secondaryindex = primaryindex


while secondaryindex > -1 {

print("comparing \(key) and \(output[secondaryindex])")
Expand Down Expand Up @@ -241,8 +244,9 @@ public class Sorting {
*/

func bubbleSortG<T: Comparable>(sequence: [T]) -> [T] {


// immediately return the trivial cases
//return trvial case
guard sequence.count > 1 else {
return sequence
}
Expand Down Expand Up @@ -341,7 +345,8 @@ public class Sorting {

func selectionSortG<T: Comparable>(sequence: [T]) -> [T] {

// immediately return the trivial cases

//check for trivial case
guard sequence.count > 1 else {
return sequence
}
Expand All @@ -352,8 +357,9 @@ public class Sorting {


for primaryindex in 0..<output.count {



//set indicies
var minimum = primaryindex
var secondaryindex = primaryindex + 1

Expand Down Expand Up @@ -384,7 +390,7 @@ public class Sorting {
}


//MARK: - Other Sorting Algorithms
//MARK: - Other Sorting Algorithms - to be refactored



Expand All @@ -393,6 +399,8 @@ public class Sorting {
// and moves values to the left or right of the pivot based on their value
// it works recursively so that either side will be eventually sorted back to the top

/*

func quickSort(sequence:[Int]) -> [Int] {

// immediately return the trivial cases
Expand Down Expand Up @@ -478,14 +486,22 @@ public class Sorting {
*/

if (leftCount < left.count && (rightCount >= right.count || left[leftCount] <= right[rightCount])) {

sortedArray.append(left[leftCount++])

} else if (rightCount < right.count && (leftCount >= left.count || right[rightCount] < left[leftCount])) {

sortedArray.append(right[rightCount++])

}
}

return sortedArray
}


*/


}


Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,46 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Source/Factories/Sorting.swift"
timestampString = "481680628.179238"
timestampString = "481755775.341086"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "135"
endingLineNumber = "135"
startingLineNumber = "138"
endingLineNumber = "138"
landmarkName = "insertionSort(_:)"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftTests/MathTest.swift"
timestampString = "481754655.821472"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "43"
endingLineNumber = "43"
landmarkName = "testFibRecursive()"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Source/Factories/Sorting.swift"
timestampString = "481756359.333058"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "489"
endingLineNumber = "489"
landmarkName = "Sorting"
landmarkType = "3">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
2 changes: 0 additions & 2 deletions SwiftTests/BloomTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,4 @@ class BloomTest: XCTestCase {





}
2 changes: 1 addition & 1 deletion SwiftTests/MathTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MathTest: XCTestCase {
let positions: Int = 9

//set the number of iterations
math.fib(positions)
math.fib(positions, sequence: [0, 1])


}
Expand Down
5 changes: 5 additions & 0 deletions SwiftTests/SortingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ class SortingTest: XCTestCase {
}


//MARK: - Other sorting algorithms - to be refactored


/*
func testQuickSort() {

let resultList: Array<Int> = sortTest.quickSort(numberList)
Expand Down Expand Up @@ -174,6 +177,8 @@ class SortingTest: XCTestCase {

}

*/



//MARK: Helper Function
Expand Down

0 comments on commit a362c9b

Please sign in to comment.