Skip to content

Commit 7578327

Browse files
authored
replaced repetitive implementation with call
1 parent d078c50 commit 7578327

File tree

1 file changed

+1
-19
lines changed

1 file changed

+1
-19
lines changed

Selection Sort/SelectionSort.playground/Sources/SelectionSort.swift

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,7 @@
33
/// - Parameter array: array of elements that conform to the Comparable protocol
44
/// - Returns: an array in ascending order
55
public func selectionSort<T: Comparable>(_ array: [T]) -> [T] {
6-
guard array.count > 1 else { return array }
7-
8-
var a = array
9-
for x in 0 ..< a.count - 1 {
10-
11-
// Find the lowest value in the rest of the array.
12-
var lowest = x
13-
for y in x + 1 ..< a.count {
14-
if a[y] < a[lowest] {
15-
lowest = y
16-
}
17-
}
18-
19-
// Swap the lowest value with the current array index.
20-
if x != lowest {
21-
a.swapAt(x, lowest)
22-
}
23-
}
24-
return a
6+
return insertionSort(array, <)
257
}
268

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

0 commit comments

Comments
 (0)