go的选择,插入,冒泡排序

点击查看代码
package main

import "fmt"

//  选择排序
func insert_sort(a [8]int) [8]int {
	for i := 1; i<len(a); i++{
		for j := i; j>0; j--{
			if a[j] < a[j-1] {
				a[j], a[j-1] = a[j-1], a[j]
			} else {
				break
			}
		}
   	}
	return a
}
 
//  选择排序
func select_sort(a [8]int) [8]int {
	for i :=0; i<len(a); i++ {
		for j :=i+1; j<len(a); j++{
			if a[j] < a[i] {
				a[i], a[j] = a[j], a[i]
			}
		}
   	}
	return a
}

//  冒泡排序
func bubble_sort(a [8]int) [8]int {
	var flag bool
	for i := 0; i<len(a); i++ {
		flag = true
		for j := 0; j<len(a)-i-1; j++{
			if a[j] > a[j+1] {
				a[j], a[j+1] = a[j+1], a[j]
				flag = false
			}
			//fmt.Printf("你好%v\n",a)
		}
		if flag {
			break
		}
   	}
	return a
}

func main(){
	var i [8]int = [8]int{8,3,2,9,4,6,10,0}
	j := insert_sort(i)
	fmt.Println(i)
	fmt.Println(j)
	
/*	var i [8]int = [8]int{8,3,2,9,4,6,10,0}
	j := select_sort(i)
	fmt.Println(i)
	fmt.Println(j)*/

/*	var i [8]int = [8]int{8,3,2,9,4,6,10,0}
	j := bubble_sort(i)
	fmt.Println("i>>", i)
	fmt.Println("j>>", j)*/
}

posted @ 2022-03-06 22:57  ty1539  阅读(19)  评论(0编辑  收藏  举报