# Golang语言常用算法

wujieyhy2006 · · 1967 次点击 · · 开始浏览

1. 堆排序

```package main

import "fmt"
func buildHeap(array []int, length int) {
var i, j int;
for i = 1; i < length; i = i + 1 {
for j = i; j > 0 && array[j] > array[(j-1)/2]; j = (j - 1)/2  {
array[j], array[(j-1)/2] = array[(j-1)/2], array[j]
}
}
}
func heapSort(array []int, length int) {
array[0], array[length - 1] = array[length - 1], array[0]
if length <= 2 {
return
}
i, j:= 0, 0
for  {
j = 2 * i + 1
if j + 1 < length - 1 {
if array[j] < array[j + 1] {
j = j + 1
}
} else if j >= length -1 {
break
}
array[i], array[j] = array[j], array[i]
i = j
}
heapSort(array, length - 1)
}
func main() {
primes := [6]int{3, 11, 5, 2, 13, 7}
fmt.Println("orginal", primes)
buildHeap(primes[:], len(primes))
fmt.Println("Max heap", primes)
heapSort(primes[:], len(primes))
fmt.Println("after sorting", primes)
}
```

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

1. 堆排序

```package main

import "fmt"
func buildHeap(array []int, length int) {
var i, j int;
for i = 1; i < length; i = i + 1 {
for j = i; j > 0 && array[j] > array[(j-1)/2]; j = (j - 1)/2  {
array[j], array[(j-1)/2] = array[(j-1)/2], array[j]
}
}
}
func heapSort(array []int, length int) {
array[0], array[length - 1] = array[length - 1], array[0]
if length <= 2 {
return
}
i, j:= 0, 0
for  {
j = 2 * i + 1
if j + 1 < length - 1 {
if array[j] < array[j + 1] {
j = j + 1
}
} else if j >= length -1 {
break
}
array[i], array[j] = array[j], array[i]
i = j
}
heapSort(array, length - 1)
}
func main() {
primes := [6]int{3, 11, 5, 2, 13, 7}
fmt.Println("orginal", primes)
buildHeap(primes[:], len(primes))
fmt.Println("Max heap", primes)
heapSort(primes[:], len(primes))
fmt.Println("after sorting", primes)
}
```