I want to make a script that does an insertion sort on the arguments provided by the user, like this:
$ insertionSort 1 2 110 39
I expect it to return:
[1 2 39 110]
But it returns:
[1 110 2 39]
I think it's because the elements in the os.Args array are strings. So, my question is how do I convert the elements of the os.Args array into integers? Here's my code:
package main
import (
"fmt"
"os"
"reflect"
"strconv"
)
func main() {
A := os.Args[1:]
for i := 0; i <= len(A); i++ {
strconv.Atoi(A[i])
fmt.Println(reflect.TypeOf(A[i]))
}
for j := 1; j < len(A); j++ {
key := A[j]
i := j - 1
for i >= 0 && A[i] > key {
A[i+1] = A[i]
i = i - 1
A[i+1] = key
}
}
fmt.Println(A)
}
As a heads up, when I substitute
strconv.Atoi(A[i])
For
A[i] = strconv.Atoi(A[i])
I get the following error:
./insertionSort.go:14: multiple-value strconv.Atoi() in single-value context
Thank you for your time!