代码拉取完成,页面将自动刷新
package main
import (
"fmt"
"math/rand"
"sort"
"time"
)
//func (r *Rand) GetRandArray(n int, max int) []int {
func CreateRandArray(n int, max int) []int {
t := time.Now()
r := rand.New(rand.NewSource(int64(t.Nanosecond())))
m := make([]int, n)
// In the following loop, the iteration when i=0 always swaps m[0] with m[0].
// A change to remove this useless iteration is to assign 1 to i in the init
// statement. But Perm also effects r. Making this change will affect
// the final state of r. So this change can't be made for compatibility
// reasons for Go 1.
for i := 0; i < n; i++ {
m[i] = r.Intn(max)
}
return m
}
func selecttSort(array []int) {
arrLen := len(array)
for i := 0; i < arrLen; i++ {
max, j := 0, 0
for j = 1; j < arrLen-i; j++ {
if array[max] < array[j] {
max = j
}
}
if max != j-1 {
array[max], array[j-1] = array[j-1], array[max]
}
// if max != 0 && max != j-1 {
// array[max], array[j-1] = array[j-1], array[max]
// }
}
}
func main() {
//r := rand.New(rand.NewSource(99))
//r := new(Rand)
//t := time.Now()
array := CreateRandArray(100000, 100000)
//fmt.Println(array)
//fmt.Println(time.Now().String())
fmt.Println(time.Now().String())
selecttSort(array)
fmt.Println(time.Now().String())
//fmt.Println(array)
array = CreateRandArray(100000, 100000)
//fmt.Println(array)
fmt.Println(time.Now().String())
sort.Ints(array)
fmt.Println(time.Now().String())
//fmt.Println(array)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。