<pre><code>
package main
import "fmt"
import "time"
import "math/rand"
func pf(arr *[]int) {
`fmt.Print("随机数组:")`
for _, data := range *arr {
fmt.Print(data, " ")
}
fmt.Print("\n")
}
func judge(arr *[]int, n int) {
for i := 0; i < n-1; i++ {
if (*arr)[i] > (*arr)[i+1] {
fmt.Println("sort fail")
return
}
}
fmt.Println("sort success")
}
func main() {
rand.Seed(time.Now().UnixNano())
n := 1000000
var arr []int
for i := 0; i < n; i++ {
arr = append(arr, rand.Intn(100))
}
//pf(&arr)
judge(&arr, n)
t := time.Now()
shell(&arr, n)
elapsed := time.Since(t)
fmt.Println("sort elapsed:", elapsed)
//pf(&arr)
judge(&arr, n)
}
func shell(arr *[]int, n int) {
ins := n
for ins > 1 {
ins = ins/3 + 1
var i, j, k int
for i = 0; i < ins; i++ {
for j = i + ins; j < n; j += ins {
temp := (*arr)[j]
for k = j - ins; k >= 0 && (*arr)[k] >= temp; k -= ins {
(*arr)[k+ins] = (*arr)[k]
}
(*arr)[k+ins] = temp
}
}
}
}
</code></pre>![深度截图_选择区域_20180407231152.png](https://static.studygolang.com/180407/c6a6a24b7427362d72c5518c0eba9889.png)
`10^6个int排序,时间有点长,请教下大神,希尔排序能否使用GO的高并发,在shell-sort函数中进行优化呢?`
有疑问加站长微信联系(非本文作者)