go语言数据结构与排序算法
package mainimport "fmt"func main() {Bubble_Sort()Select_Sort()Insert_Sort()Shell_Sort()Heap_Sort()
}
冒泡排序
// 冒泡排序
func Bubble_Sort() {str := []int{9, 1, 5, 8, 3, 7, 4, 6, 2}// 正向冒泡for i := 0; i < len(str)-1; i++ {for j := 1; j <= len(str)-1; j++ {if str[j-1] > str[j] {str[j-1], str[j] = str[j], str[j-1]}}}// 反向冒泡for i := 0; i < len(str)-1; i++ {for j := len(str) - 1; j > i; j-- {if str[j-1] > str[j] {str[j-1], str[j] = str[j], str[j-1]}}}//改进for i := 0; i < len(str)-1; i++ {flag := falsefor j := 1; j <= len(str)-1; j++ {if str[j-1] > str[j] {str[j-1], str[j] = str[j], str[j-1]flag = true}}if !flag {break}}fmt.Println(str)
}
选择排序
// 选择排序
func Select_Sort() {str := []int{9, 1, 5, 8, 3, 7, 4, 6, 2}for i := 0; i < len(str)-1; i++ {// 未排序区间最小值初始化为第一个元素min := i// 从未排序区间第二个元素开始遍历,直到找到最小值for j := i + 1; j <= len(str)-1; j++ {if str[min] > str[j] {min = j}}// 将最小值与未排序区间第一个元素互换位置(等价于放到已排序区间最后一个位置)if min != i {str[min], str[i] = str[i], str[min]}}fmt.Println(str)
}
插入排序
//插入排序
func Insert_Sort() {str := []int{9, 1, 5, 8, 3, 7, 4, 6, 2}for i := 1; i <= len(str)-1; i++ {temp := str[i]j := i - 1for ; j >= 0 && str[j] > temp; j-- {str[j+1] = str[j]}str[j+1] = temp}fmt.Println(str)
}
希尔排序
//希尔排序 与插入排序比较,把原来按顺序变成了相隔增量
func Shell_Sort() {str := []int{9, 1, 5, 8, 3, 7, 4, 6, 2}//increment相隔数量// for increment := len(str) / 3; increment > 0; increment /= 3 {increment := len(str)for increment > 0 {increment = increment / 3//此过程类似于插入排序的过程for i := increment; i <= len(str)-1; i++ {key := str[i]j := i - increment//按照increment,数组从j到0进行交换比较for ; j >= 0 && str[j] > key; j -= increment {str[j+increment] = str[j]}//如果是从for循环走到这里,此时j<0,因为for循环走完时j-=increment ,所以要加回来//走到这里时,j已经减掉increment 了,所以要加回来str[j+increment] = key}}fmt.Println(str)
}
堆排序
//堆排序
func Heap_Sort() {str := []int{9, 1, 5, 8, 3, 7, 4, 6, 2}//构建一个大顶堆for i := len(str) / 2; i > 0; i-- {Heap_Adjust_2(str, len(str), i)}for i := len(str) - 1; i > 0; i-- {str[0], str[i] = str[i], str[0]Heap_Adjust_2(str, i, 0) //将剩余的重新调整为大顶堆}fmt.Println(str)
}//堆调整函数0
func Heap_Adjust_0(str []int, length, index int) {temp := str[index]for j := 2*index + 1; j <= length-1; j *= 2 { //沿关键字较大的孩子节点向下筛选if j < length-1 && str[j] < str[j+1] {j++ //j记录孩子节点较大的下标}if temp > str[j] { //父节点大于孩子节点break}str[index] = str[j] //给节点赋值index = j //index此时已代表孩子节点下标}str[index] = temp //给孩子节点赋值,到此代表节点的值跟孩子节点中的较大值进行互换完成}//堆调整函数1
func Heap_Adjust_1(str []int, length, index int) {parent := indexchild := parent*2 + 1for ; child <= length-1; child *= 2 {if child < length-1 && str[child+1] > str[child] {child++}if str[child] > str[parent] {str[child], str[parent] = str[parent], str[child]parent = child}}
}//堆调整函数2
func Heap_Adjust_2(str []int, length, index int) {largest := index // 初始化最大元素为根节点left := 2*index + 1 // 左子节点索引right := 2*index + 2 // 右子节点索引// 如果左子节点大于根节点if left < length && str[left] > str[largest] {largest = left}// 如果右子节点大于最大元素if right < length && str[right] > str[largest] {largest = right}// 如果最大元素不是根节点if largest != index {str[index], str[largest] = str[largest], str[index]// 递归调整受影响的子树Heap_Adjust_2(str, length, largest)}
}