Python
def selection_sort(array):
for i in range(0, len(array) - 1):
min_index = i
for j in range(i + 1, len(array)):
if array[j] < array[min_index]:
min_index = j
array[i], array[min_index] = array[min_index], array[i]
n = int(input())
a = list(map(int, input().split()))
selection_sort(a)
print(*a)
Swift
func selectionSort (array: [Int]) -> [Int] {
var a = array
var minIndex = 0
for i in (0...a.count - 2) {
minIndex = i
for j in (i + 1...a.count - 1) {
if a[j] < a[minIndex] {
minIndex = j
}
}
let temp = a[i]
a[i] = a[minIndex]
a[minIndex] = temp
}
return a
}
var n: Int = 0
var array: [Int] = []
if let input = readLine(){
n = Int(input)!
}
if let input = readLine(){
array = input.split(separator: " ").map{Int($0)!}
}
let res = selectionSort(array: array)
print(res.map{String($0)}.joined(separator: " "))
|