import Foundation
class SelectSortSolution {
func sort(_ n: [Int]) -> [Int] {
var s = n
for i in 0..<s.count {
let j = i + 1
var min = s[i]
var indexMin = i
for r in j..<s.count {
if min > s[r] {
min = s[r]
indexMin = r
}
}
if indexMin != i {
let temp = s[i]
s[i] = s[indexMin]
s[indexMin] = temp
}
}
return s
}
}
算法思想:每一趟从待排序的数据元素中选择最小(或最大)的一个元素作为首元素,直到所有元素排完为止,简单选择排序是不稳定排序。
github地址:https://github.com/cubegao/LeetCode