Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Solution for assignment gmertk#2 - Insertion Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
diegopetrucci committed Nov 29, 2015
1 parent eabbc27 commit 82299e3
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,32 @@ Note that there is already a `swap` function in the standard library. However, y

func insertionSort<T>(var array: [T], @noescape isOrderedBefore: (T, T) -> Bool) -> [T] {

guard array.count > 1 else {
return array
}

var i = 0
var j = 1

repeat {
repeat {
if isOrderedBefore(array[i], array[j]) == false {
let itemToBeChanged = array[j]
array.removeAtIndex(j)
array.insert(itemToBeChanged, atIndex: i)
i = 0
j = 1
print("found swapping")
} else { j++ }
} while j < array.count - 1
i++
j = i + 1
print("moving index i")
} while i < array.count - 1

return array
}


//: Test your function with assert. Make sure asserts don't raise any errors. `isSorted` is already defined for you in `Sources/Utilities.swift`. You can add more test cases.

let items = ["c", "d", "b"]
Expand Down

0 comments on commit 82299e3

Please sign in to comment.