forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
682 B
30 lines
682 B
package main
|
|
|
|
// 选择排序
|
|
func main() {
|
|
var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明
|
|
Select(a)
|
|
|
|
for _, i2 := range a {
|
|
print(i2, "\t") // 输出每一个值
|
|
}
|
|
}
|
|
|
|
/**
|
|
Select 实现选择排序 O(n * n)
|
|
a : 被排序数组
|
|
返回:排序完毕数组
|
|
*/
|
|
func Select(a []int) {
|
|
for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素
|
|
theMin := i // 记录最小值记录
|
|
for ii := i + 1; ii < j; ii++ { // 循环无序列表部分
|
|
if a[theMin] > a[ii] { // 较大的值移至后边
|
|
theMin = ii // 较大的值移至后边
|
|
}
|
|
}
|
|
if theMin != i {
|
|
a[i], a[theMin] = a[theMin], a[i]
|
|
}
|
|
}
|
|
}
|
|
|