Golang中的插入排序

问题描述:

package main
    import(
      "fmt"
    )

Output an element of the sequence in turn.

func trace(A[]int,N int){
  for i :=0; i < N; i++{
    if i > 0 {
      fmt.Println(" ")
      fmt.Println("%d",A[i])
    }
  }
    fmt.Println("
")
}

0-rigin

func insertionSort(A[]int, N int) (int int) {
      for i := 0; i < N; i++{
        v := A[i]
        j := i -  1
        for j >= 0 && A[j] > v{
          A[j + 1] = A[j]
          j--
        }
          A[ j + 1 ] = v
          trace(A,N)
      }
      return int
    }

The above code has an error and the code below also has an error.

   func main() {
      var N,i,j int
      var A[100]int
      scanf("%d",&N)
      for i:= 0;  i < N; i++ {
        scanf("%d",&A[i])
      }
      fmt.Println(trace(A,N))
      fmt.Println(insertionSort(A,N))

      return 0
    }

This is all in C and lots of the errors occured in Main.

go1.5.2

A couple of things:

  • scanf isn't a function (you probably want fmt.Scanf)
  • arrays aren't slices ( [100]int is a different type than []int)
  • you can't print trace(A,N) because it doesn't return a value