//////////////////////////////////////////////////////////////////////////////////////////////////// // filename : main.go // author : LiuDongguo // contact : jealdean@outlook.com // created : 21:39 16 Sep 2014 //////////////////////////////////////////////////////////////////////////////////////////////////// package main import ( "fmt" ) func main() { arrSlice := []int{5, 1, 1, 4, 4, 4, 3, 7, 5, 1} al := len(arrSlice) for i := 0; i < al-1; i++ { SelectTwoMinimum(arrSlice) } } //sort the array by Insert-Sort,one sort algorithm of stable func insertSort(arr []int) { j, total := 0, len(arr) for i := 1; i < total; i++ { tmp := arr[i] for j = i - 1; j >= 0 && arr[j] > tmp; j-- { arr[j+1] = arr[j] } arr[j+1] = tmp } } func SelectTwoMinimum(arr []int) (min, minSec int) { insertSort(arr) fmt.Println("After sort:", arr) i, arrLen := 0, len(arr) for ; i < arrLen; i++ { if arr[i] != 0 { break } } min, minSec = arr[i], arr[i+1] arr[i], arr[i+1] = 0, minSec+min fmt.Printf("the two minimum value: (%d,%d)\nAtfter value reset:%v\n", min, minSec, arr) return }
有疑问加站长微信联系(非本文作者)