一直覺(jué)得自己的數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)的不是很好, 抽空練習(xí)練習(xí)下基礎(chǔ)算法.
先來(lái)張圖,了解下算法
cdn2.b0.upaiyun.com/2012/01/Visual-and-intuitive-feel-of-7-common-sorting-algorithms.gif">
這里是golang的代碼實(shí)現(xiàn),歡迎拍磚,
目前成都創(chuàng)新互聯(lián)公司已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、通山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
算法比較簡(jiǎn)單, 就是在出棧的節(jié)點(diǎn)上想了半天,這點(diǎn)歡迎
package main
import (
"fmt"
)
const (
greaterthan = iota //大于
equal // 等于
lessthan //小于
)
//比較
func comparedata(d1 int, d2 int) int {
if d1 < d2 {
return lessthan
} else if d1 == d2 {
return equal
} else {
return greaterthan
}
}
//交換
func swap(d1 *int, d2 *int) {
var temp int
temp = *d1
*d1 = *d2
*d2 = temp
return
}
//排序
func quicksort(datalist []int, left int, right int) {
var middle int
middle = (left + right) / 2
if middle == left {
return
}
if middle == right {
return
}
fmt.Println("left ", left, "right ", right)
for i := left; i <= middle; i++ {
for j := middle; j <= right; j++ {
if comparedata(datalist[i], datalist[j]) == greaterthan {
swap(&datalist[i], &datalist[j])
}
}
}
if left < middle {
quicksort(datalist, left, middle)
}
if middle < right {
quicksort(datalist, middle, right)
}
}
func sort(datalist []int) {
quicksort(datalist, 0, len(datalist)-1)
fmt.Println("數(shù)據(jù):")
fmt.Println(datalist)
}
const (
k = 200
)
func main() {
var (
datalist []int
)
datalist = make([]int, k)
for i := 0; i < k; i++ {
datalist[i] = 100 - i
}
datalist[1] = 101
datalist[2] = 103
datalist[8] = 200
datalist[6] = 201
datalist[4] = 204
datalist[5] = 206
datalist[3] = 207
datalist[9] = 2009
datalist[7] = 250
fmt.Println(datalist)
sort(datalist)
}