Skip to content

Commit

Permalink
- Continued work on sorting algorithms..
Browse files Browse the repository at this point in the history
- implemented swap() for bubbleSort
- additional unit tests for generic sorting algorithms
  • Loading branch information
waynewbishop committed Dec 2, 2015
1 parent 82a4754 commit c22de60
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
16 changes: 8 additions & 8 deletions Source/Factories/Sorting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ public class Sorting {
insertion sort algorithm - (Generics)
*/

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


//mutated copy
var output = Array(numberList)
var output = Array(sequence)


for primaryIndex in 0..<output.count {
Expand All @@ -200,7 +200,7 @@ public class Sorting {

for var secondaryIndex = primaryIndex; secondaryIndex > -1; secondaryIndex-- {

print("comparing \(key) and \(numberList[secondaryIndex])")
print("comparing \(key) and \(sequence[secondaryIndex])")

if key < output[secondaryIndex] {

Expand Down Expand Up @@ -268,14 +268,14 @@ public class Sorting {
bubble sort algorithm - (Generics)
*/

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


//mutated copy
var output = Array(numberList)
var output = Array(sequence)


for primaryIndex in 0..<numberList.count {
for primaryIndex in 0..<sequence.count {


let passes = (output.count - 1) - primaryIndex
Expand Down Expand Up @@ -353,11 +353,11 @@ public class Sorting {
selection sort algorithm - (Generics)
*/

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


//mutated copy
var output = Array(numberList)
var output = Array(sequence)


for primaryIndex in 0..<output.count {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,22 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftTests/SortingTest.swift"
timestampString = "470775684.155673"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "105"
endingLineNumber = "105"
landmarkName = "testBubbleSort()"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
26 changes: 18 additions & 8 deletions SwiftTests/SortingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ class SortingTest: XCTestCase {


private var numberList: Array<Int>!
private var textList: Array<String>!
private var sortTest: Sorting!



override func setUp() {
super.setUp()

numberList = [8, 2, 10, 9, 7, 5]
textList = ["Dog", "Cat", "Dinasour", "Lion", "Cheetah", "Gazelle", "Elephant", "Aardvark"]
sortTest = Sorting()
}

Expand Down Expand Up @@ -54,7 +56,7 @@ class SortingTest: XCTestCase {
*/

func testBinarySearchClosure() {


var searchList: Array<Int> = Array<Int>()
var isFound: Bool = false
Expand Down Expand Up @@ -86,20 +88,26 @@ class SortingTest: XCTestCase {
func testInsertionSort() {

let resultList: Array<Int> = sortTest.insertionSort(numberList)
let sequence = sortTest.insertionSortG(textList)


//evaluate results
XCTAssertTrue(self.IsSorted(resultList), "item sequence not in sorted order..")
XCTAssertTrue(self.IsSorted(sequence), "item sequence not in sorted order..")

}



func testBubbleSort() {

let resultList: Array<Int> = sortTest.bubbleSort(numberList)

let resultsList: Array<Int> = sortTest.bubbleSort(numberList)
let sequence = sortTest.bubbleSortG(textList)


//evaluate results
XCTAssertTrue(self.IsSorted(resultList), "item sequence not in sorted order..")
XCTAssertTrue(self.IsSorted(resultsList), "item sequence not in sorted order..")
XCTAssertTrue(self.IsSorted(sequence), "item sequence not in sorted order..")

}

Expand All @@ -108,10 +116,12 @@ class SortingTest: XCTestCase {
func testSelectionSort() {

let resultList: Array<Int> = sortTest.selectionSort(numberList)
let sequence = sortTest.insertionSortG(textList)


//evaulate results
XCTAssertTrue(self.IsSorted(resultList), "item sequence not in sorted order..")

XCTAssertTrue(self.IsSorted(sequence), "item sequence not in sorted order..")

}

Expand Down Expand Up @@ -142,8 +152,8 @@ class SortingTest: XCTestCase {
//MARK: Helper Function


//determine sorted list
func IsSorted(sequence: Array<Int>) -> Bool {
//generic method to determine sorted order
func IsSorted<T: Comparable>(sequence: [T]) -> Bool {


for var primaryIndex = 0; primaryIndex < sequence.count; primaryIndex++ {
Expand Down

0 comments on commit c22de60

Please sign in to comment.