Skip to content

Commit

Permalink
replaced repetitive implementation with call
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioBBL authored Oct 16, 2018
1 parent d078c50 commit 7578327
Showing 1 changed file with 1 addition and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,7 @@
/// - Parameter array: array of elements that conform to the Comparable protocol
/// - Returns: an array in ascending order
public func selectionSort<T: Comparable>(_ array: [T]) -> [T] {
guard array.count > 1 else { return array }

var a = array
for x in 0 ..< a.count - 1 {

// Find the lowest value in the rest of the array.
var lowest = x
for y in x + 1 ..< a.count {
if a[y] < a[lowest] {
lowest = y
}
}

// Swap the lowest value with the current array index.
if x != lowest {
a.swapAt(x, lowest)
}
}
return a
return insertionSort(array, <)
}

/// Performs the Selection sort algorithm on a array using the provided comparisson method
Expand Down

0 comments on commit 7578327

Please sign in to comment.