Insertion short


(andre nunes nachtigall) #1

I’m having difficulty creating a random value insert in GO, I need to do 10 tests with 1000 numbers, then 10 tests with 5000 numbers, then 10 tests with 10000 numbers, then 10 tests with 15000 … up to 50000

I made a code with function and another code without function and the 2 are wrong

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)
	}
}

(Jakob Borg) #2

Can you reduce this to a runnable test case on play.golang.org please?


(andre nunes nachtigall) #3

https://play.golang.org/p/QvvTKAcTv2s


(Curtis Allyn Green) #4

You need to use

var vetor = make([]int, 50000)

To set your empty slices.


(system) #5

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.