首页 > 其他分享 >Go-堆排序

Go-堆排序

时间:2022-12-15 12:23:49浏览次数:34  
标签:arr int fmt 堆排序 lastmesslen length topmax Go

package main

import "fmt"

func HeapSort(arr []int) []int {
    length := len(arr)
    for i := 0; i < length; i++ {
        lastmesslen := length - i     //每次截取一段
        HeapSortMax(arr, lastmesslen) //0-----------0
        fmt.Println(arr)
        if i < length {
            arr[0], arr[lastmesslen-1] = arr[lastmesslen-1], arr[0]
        }
        fmt.Println("ex", arr)
    }
    return arr

}

func HeapSortMax(arr []int, length int) []int {
    //length:=len(arr)//数组长度
    if length <= 1 {
        return arr //一个元素的数组,直接返回
    } else {
        depth := length/2 - 1         // 深度, n  2*n+1,2*n+2
        for i := depth; i >= 0; i-- { //循环所有的三节点
            topmax := i //假定最大的在i的位置
            leftchild := 2*i + 1
            rightchild := 2*i + 2                                      //左右孩子的节点
            if leftchild <= length-1 && arr[leftchild] > arr[topmax] { //防止越界
                topmax = leftchild //如果左边比我大,记录最大
            }
            if rightchild <= length-1 && arr[rightchild] > arr[topmax] {
                topmax = rightchild //如果右边比我大,记录最大
            }
            if topmax != i { //确保i的值就是最大
                arr[i], arr[topmax] = arr[topmax], arr[i]
            }

        }
        return arr

    }

}

func main() {
    arr := []int{1, 9, 2, 8, 3, 7, 4, 6, 5, 10}
    //fmt.Println(SelectSortMax(arr))
    fmt.Println(HeapSort(arr))

}

 

标签:arr,int,fmt,堆排序,lastmesslen,length,topmax,Go
From: https://www.cnblogs.com/Essaycode/p/16984689.html

相关文章

  • Go--并发编程
    摘抄(有删改):https://www.topgoer.cn/docs/golang/chapter09-1一、并发介绍1.1进程和线程进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单位......
  • vue+django项目nginx部署在https下
    vue+django项目nginx部署在https下1.问题出现这个问题的原因是在https网站下浏览器不允许发送http请求。由于django默认是http,所以肯定会报这个错误,如果网站部署在http......
  • chaosblade-exec-os项目的burnio.go文件解读
    #################################################代码位置:​​https://github.com/chaosblade-io/chaosblade-exec-os.git​​文件位置:chaosblade-exec-os/exec/bin/burn......
  • gorilla/mux
    ##############地址:​​https://github.com/gorilla/mux​​   安装goget-ugithub.com/gorilla/mux 使用添加包引用:"github.com/gorilla/mux" 常用方法介绍初始化路......
  • Go语言性能剖析利器--pprof实战
    作者:耿宗杰前言关于pprof的文章在网上已是汗牛充栋,却是千篇一律的命令介绍,鲜有真正实操的,本文将参考Go社区资料,结合自己的经验,实战Go程序的性能分析与优化过程。优化思路首......
  • mongodb的collection方法
       方法名描述​​db.collection.aggregate()​​聚合,主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果​​db.collection.bulkWrite()​​批量写入​​d......
  • mongodb的db方法
          方法名描述​​db.cloneDatabase()​​从指定主机上克隆数据库​​db.currentOp()​​显示当前正在进行的操作​​db.commandHelp()​​返回数据库命令的帮助信......
  • 搭建mongodb分片集群
            注意:mongos、config、shard三个角色的实例的keyfile内容保证完全一致: 如果搭建副本集时,出错,那么删掉     config副本集配置文件内容:使用mongod启动:[w......
  • golang的module管理与使用go mod
    #############################  更换或升级了golang后,需要删除go.mod、go.sum、vendor文件,然后重建,不然一直卡在那里      使用: Gomodules操作命令及相关文件解......
  • golang.mysql
    一、mysql操作基本语法1、创建名称nulige的数据库 ​​CREATEDATABASEnuligeDEFAULTCHARSETutf8COLLATEutf8_general_ci;<br><br>usenulige​​2、建表,Id自增​​c......