标签:print 偶数 turn false pack for package sort pac
核心思想:奇数位和偶数位的数据交替比较,最终实现排序
代码实现:
package main
import "fmt"
func IsSort(arr []int) []int {
flag := false
if len(arr) <= 1 {
return arr
}
for flag == false {
flag = true
for i := 0; i < len(arr)-1; i = i + 2 {
if arr[i] > arr[i+1] {
arr[i], arr[i+1] = arr[i+1], arr[i]
flag = false
}
}
for i := 1; i < len(arr)-1; i = i + 2 {
if arr[i] > arr[i+1] {
arr[i], arr[i+1] = arr[i+1], arr[i]
flag = false
}
}
}
return arr
}
func main() {
arr := []int{5, 555, 4, 7, 5, -7, 99}
fmt.Println(IsSort(arr))
}
标签:print 偶数 turn false pack for package sort pac
原文地址:https://www.cnblogs.com/ydg-941020/p/14484662.html