A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Selection Sort in Swift page! Here, you'll find the source code for this program as well as a description of how the program works.
import Foundation
let usage = """
Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"
"""
extension StringProtocol {
var trimmed: String { trimmingCharacters(in: .whitespacesAndNewlines) }
}
func parseIntegers(from args: [String]) -> [Int]? {
guard args.count == 2 else { return nil }
let parts = args[1].split(separator: ",", omittingEmptySubsequences: false)
let values = parts.compactMap { Int($0.trimmed) }
guard values.count == parts.count,
values.count >= 2
else {
return nil
}
return values
}
extension MutableCollection where Self: BidirectionalCollection {
mutating func selectionSort(by areInIncreasingOrder: (Element, Element) -> Bool) {
guard count > 1 else { return }
var i = startIndex
while i != index(before: endIndex) {
var minIndex = i
var j = index(after: i)
while j != endIndex {
if areInIncreasingOrder(self[j], self[minIndex]) {
minIndex = j
}
j = index(after: j)
}
if minIndex != i {
swapAt(i, minIndex)
}
i = index(after: i)
}
}
}
guard var numbers = parseIntegers(from: CommandLine.arguments) else {
print(usage)
exit(1)
}
numbers.selectionSort(by: <)
print(numbers.map(String.init).joined(separator: ", "))
Selection Sort in Swift was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.