当前位置: 首页 > news >正文

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)}
}

 

http://www.dtcms.com/a/294309.html

相关文章:

  • 【C++】C++ 的入门知识2
  • Android 持久化存储原理与使用解析
  • MATLAB | 绘图复刻(二十二)| 带树状图的三角热图合集
  • 个性化网页计数器
  • C 语言介绍
  • 【数据结构】二叉树的链式结构--用C语言实现
  • 嵌入式linux下的NES游戏显示效果优化方案:infoNES显示效果优化
  • 我用EV-21569-SOM评估来开发ADSP-21569(十三)-SigmaStudio Plus做开发(4)
  • Web前端开发:JavaScript遍历方法详解与对比
  • 安全防护-FCW
  • [HarmonyOS] HarmonyOS LiteOS-A 设备开发全流程指南
  • Linux第三天Linux基础命令(二)
  • 服务器对kaggle比赛的数据集下载
  • SAP-ABAP:SELECT语句验证字段和验证方法详解
  • OSPF路由协议——上
  • 28. 找出字符串中第一个匹配项的下标
  • vue3中el-table表头筛选
  • Flink 状态管理设计详解:StateBackend、State、RocksDB和Namespace
  • 谷粒商城篇章13--P340-P360--k8s/KubeSphere【高可用集群篇一】
  • 抖音集团基于Flink的亿级RPS实时计算优化实践
  • k8s pvc是否可绑定在多个pod上
  • 飞算JavaAI:从“工具革命”到“认知革命”——开发者如何借力AI重构技术竞争力
  • SpringBoot 内嵌 Tomcat 的相关配置
  • MySQL binlog解析
  • linux c语言进阶 - 线程,通信方式,安全方式(多并发)
  • Linux中常见的中英文单词对照表
  • 低代码中的统计模型是什么?有什么作用?
  • 第一二章知识点
  • 交换机的六种常见连接方式配置(基于华为eNSP)
  • 洛谷刷题7.23