当前位置: 首页 > 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.lryc.cn/news/597042.html

相关文章:

  • Http证书体系及证书加密流程(通信流程)
  • Web开发基础与RESTful API设计实践指南
  • kafka动态配置详解
  • 基于Kafka实现动态监听topic功能
  • 变频器实习DAY12
  • (一)从零搭建unity3d机械臂仿真-unity3d导入urdf模型
  • Kafka——Kafka中的位移提交
  • git 修改最近一次 commit 信息
  • 【2025】使用vue构建一个漂亮的天气卡片
  • Dify实战,获取禅道需求,编写测试用例到禅道
  • [AI8051U入门第八步]硬件IIC驱动AHT10温湿度传感器
  • Web 服务器和Web 中间件
  • 主流软件开发方法综述:从敏捷到开源
  • 利用中间件实现任务去重与分发精细化:股吧舆情数据采集与分析实战
  • 如何高效合并音视频文件
  • 设计模式九:构建器模式 (Builder Pattern)
  • echarts【实战】饼状图点击高亮,其他区域变暗
  • flutter使用CupertinoPicker绘制一个传入数据源的省市区选择器
  • [Bug | Cursor] import error: No module named ‘data‘
  • C++刷题 - 7.23
  • 【C++】类和对象(中)构造函数、析构函数
  • nrm指南
  • 二级建造师学习笔记-2025
  • 2025 成都航空装备展供需发布:精准匹配,高效成交
  • 货车手机远程启动功能的详细使用步骤及注意事项
  • C#值类型属性的典型问题
  • 基于.Net Core开源的库存订单管理系统
  • 【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 主页-微博点赞量Top6实现
  • 粗大误差智能滤除:基于格拉布斯准则与机器学习的数据清洗体系​
  • 深入理解 TCP 协议:Linux 网络传输的可靠基石