/* 直接插入排序 */ func straightInsertion(array []int) { for i := 1; i < len(array); i++ { temp := array[i] if array[i] < array[i-1] { j := i - 1 for j >= 0 && temp < array[j] { array[j+1] = array[j] j-- } array[j+1] = temp } } } func main() { array := []int{6, 5, 4, 9, 8, 3, 7, 2, 1} straightInsertion(array) fmt.Println(array) }