在學(xué)習(xí)golang語(yǔ)言燃少,文檔看的實(shí)在是乏味,就想著把常用的算法做個(gè)實(shí)現(xiàn)铃在,邊寫變學(xué)習(xí)阵具,想來(lái)效果還是不錯(cuò)的定铜!
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)
}
/**
out:
orginal [3 11 5 2 13 7]
Max heap [13 11 7 2 3 5]
after sorting [2 3 5 7 11 13]
**/
2.冒泡排序
package main
import "fmt"
func BubbleSort(vector []int) {
fmt.Println("BubbleSort")
fmt.Println(vector)
for i := 0; i < len(vector); i++ {
tag := true // 為了剪枝
// 每一趟將最大的數(shù)冒泡
for j := 0; j < len(vector)-i-1; j++ {
if vector[j] > vector[j+1] { /*vector[j] < vector[j+1]*/
temp := vector[j]
vector[j] = vector[j+1]
vector[j+1] = temp
tag = false
}
}
if tag {
break //0~len(vector)-i沒(méi)有發(fā)生交換說(shuō)明已經(jīng)有序
}
fmt.Println(vector)
}
}
func main() {
primes := [6]int{3, 11, 5, 2, 13, 7}
fmt.Println("orginal", primes)
BubbleSort(primes[:])
fmt.Println("after sorting", primes)
}
/**
out:
orginal [3 11 5 2 13 7]
BubbleSort
[3 11 5 2 13 7]
[3 5 2 11 7 13]
[3 2 5 7 11 13]
[2 3 5 7 11 13]
after sorting [2 3 5 7 11 13]
**/
3. 插入排序
package main
import "fmt"
func InsertSort(vector []int) {
fmt.Println("InsertSort")
fmt.Println(vector)
for i := 1; i < len(vector); i++ {
// 每一趟不滿足條件就選擇i為哨兵保存,將哨兵插入0~i-1有序序列(0~i-1始終是有序的)
if vector[i] < vector[i-1] { /*vector[i] > vector[i-1]*/
temp := vector[i]
//后移直到找到哨兵合適的位置
j := i - 1
for ; j >= 0 && vector[j] > temp; j-- { /*vector[j] < temp*/
vector[j+1] = vector[j]
}
//插入位置前后都是有序的,最后也是有序的
vector[j+1] = temp
}
fmt.Println(vector)
}
}
func main() {
primes := [6]int{3, 11, 5, 2, 13, 7}
fmt.Println("orginal", primes)
InsertSort(primes[:])
fmt.Println("after sorting", primes)
}
/**
out:
orginal [3 11 5 2 13 7]
InsertSort
[3 11 5 2 13 7]
[3 11 5 2 13 7]
[3 5 11 2 13 7]
[2 3 5 11 13 7]
[2 3 5 11 13 7]
[2 3 5 7 11 13]
after sorting [2 3 5 7 11 13]
**/
4. 選擇排序
package main
import "fmt"
func SelectSort(vector []int) {
fmt.Println("SelectSort")
fmt.Println(vector)
for i := 0; i < len(vector); i++ {
// 選擇最小的元素
k := i
for j := i + 1; j < len(vector); j++ {
if vector[k] > vector[j] {
k = j
}
}
// 交換
if k != i {
temp := vector[i]
vector[i] = vector[k]
vector[k] = temp
}
fmt.Println(vector)
}
}
func main() {
primes := [6]int{3, 11, 5, 2, 13, 7}
fmt.Println("orginal", primes)
SelectSort(primes[:])
fmt.Println("after sorting", primes)
}
/**
out:
orginal [3 11 5 2 13 7]
SelectSort
[3 11 5 2 13 7]
[2 11 5 3 13 7]
[2 3 5 11 13 7]
[2 3 5 11 13 7]
[2 3 5 7 13 11]
[2 3 5 7 11 13]
[2 3 5 7 11 13]
after sorting [2 3 5 7 11 13]
**/
5. 二元選擇排序
package main
import "fmt"
func BinarySelectSort(vector []int) {
fmt.Println("SelectSort")
fmt.Println(vector)
n := len(vector)
for i := 0; i < n/2; i++ {
// 選擇最小的元素和最大元素
k := i
t := n - i - 1
for j := i + 1; j <= n-i-1; j++ {
if vector[k] > vector[j] {
k = j
}
if vector[t] < vector[j] {
t = j
}
}
// 交換
if k != i {
temp := vector[i]
vector[i] = vector[k]
vector[k] = temp
}
if t != n-i-1 {
temp := vector[n-i-1]
vector[n-i-1] = vector[t]
vector[t] = temp
}
fmt.Println(vector)
}
}
func main() {
primes := [6]int{3, 11, 5, 2, 13, 7}
fmt.Println("orginal", primes)
BinarySelectSort(primes[:])
fmt.Println("after sorting", primes)
}
/**
out:
orginal [3 11 5 2 13 7]
SelectSort
[3 11 5 2 13 7]
[2 11 5 3 7 13]
[2 3 5 11 7 13]
[2 3 5 11 7 13]
after sorting [2 3 5 11 7 13]
**/