Go语言实现插入排序

package main                                                                                                                                                                                                                                

import (                                                                                                                  
	"fmt"                                                                                                             
)                                                                                                                                                                                                                                           
func BubbleSort(num []int) {                                                                                              
	for i := 1; i < len(num); i++ {                                                                                           
		for j := i; j > 0; j-- {                                                                                                  
			if (num[j] < num[j-1]) {                                                                                                  
				num[j], num[j - 1] = num[j - 1], num[j]                                                                           
			} else if (num[j] > num[j - 1]) {                                                                                         
				break
            }                                                                                                                 
        }                                                                                                                 
    }                                                                                                                 
}                                                                                                                                                                                                                                           
func main() {                                                                                                             
	a := [...] int {1,5684,25,487,74,2,54894,51,65874,51,65,478}                                                          
	BubbleSort(a[:])                                                                                                      
	fmt.Println(a)                                                                                                    
}                    
posted @ 2019-08-29 09:22  AlexKing007  阅读(98)  评论(0编辑  收藏  举报