使用Go

时间:2018-05-15 00:40:27

标签: arrays go insertion-sort

我在Go中创建随机值插入有困难,我需要用1000个数字进行10次测试,然后用5000个数字进行10次测试,然后用10000次测试进行10次测试,然后用15000次进行10次测试...高达50000

我创建了一个带有函数的代码和另一个没有函数的代码,而且2是错误的

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    var vetor [50000]int
    for k := 1; k <= 10; {
        k := (5 ^ k) * 1000
        for i := 1000; i < 50000; {
            vetor[i] = rand.Intn(1000)
            fmt.Print(vetor[i], ",")
        }
        start := time.Now()
        n := len(vetor)
        if n < 2 {
            return
        }
        for i := 1; i < n; i++ {
            for j := i - 1; j >= 0; j-- {
                if (vetor)[j] > (vetor)[j+1] {
                    (vetor)[j], (vetor)[j+1] = (vetor)[j+1], (vetor)[j]
                } else {
                    break
                }
            }
        }
        t := time.Now()
        decorrido := t.Sub(start)
    }
}
package main

import (
    "fmt"
    "math/rand"
    "time"
)

func insertion(array *[]int) {
    n := len(*array)
    if n < 2 {
        return
    }
    for i := 1; i < n; i++ {
        for j := i - 1; j >= 0; j-- {
            if (*array)[j] > (*array)[j+1] {
                (*array)[j], (*array)[j+1] = (*array)[j+1], (*array)[j]
            } else {
                break
            }
        }
    }
}

func main() {
    var vetor [50000]int
    for k := 1; k <= 10; {
        k := (5 ^ k) * 1000
        for i := 1000; i < 50000; {
            vetor[i] = rand.Intn(1000)
            fmt.Print(vetor[i], ",")
        }
        start := time.Now()
        insertion(&vetor)
        t := time.Now()
        decorrido := t.Sub(start)
    }
}

我还需要计算所有测试的运行时间,并从所有10个结果中取出平均值

0 个答案:

没有答案