-1

Please help me implement bubble sort.It works fine if I call it with a hardcoded slice from the main() but if I call it with dynamic input from Scan it breaks

here is my code so far:

package main

import "fmt"

func main() {
    fmt.Println("Enter a maximum of 10 numbers: ")

    var inputs int
    fmt.Scanln(&inputs)
    inputSlice := make([]int, inputs)

    BubbleSort(inputSlice)

    fmt.Println(inputSlice)

}

func BubbleSort(input []int) {

    for i := 0; i < len(input)-1; i++ {
        for j := 0; j < len(input)-i-1; j++ {
            Swap(input, j)
        }
    }
}

func Swap(input []int, j int) {
    if input[j] > input[j+1] {
        input[j], input[j+1] = input[j+1], input[j]
    }
}

terminal:

coder:~/project$ go run bubblesort.go
Enter a maximum of 10 numbers:
12 24 54 65 11
coder:~/project$ 4 54 65 11
bash: 4: command not found
shouravRahman
  • 17
  • 1
  • 4
  • Why do you delete the previous question and literally copy-paste the same question again? – tkausl Nov 14 '21 at 12:17
  • How does it break? What is the error message? The second error in your terminal occurred because you didn't run the program first. – aside Nov 14 '21 at 12:23
  • 1
    @shouravRahman: You are reading an integer, not a sequence of integers (`var inputs int`). That's why it stops reading after it detected an integer and the rest is just interpreted as shell input once the command has exited. – Steffen Ullrich Nov 14 '21 at 12:34
  • i ran it one time and then it prompts for a input.after giving inputs i hit enter again and then breaks.what am i missing here@SteffenUllrich – shouravRahman Nov 14 '21 at 12:37
  • 1
    Does this answer your question? [Read a set of integers separated by space in Golang](https://stackoverflow.com/questions/39565055/read-a-set-of-integers-separated-by-space-in-golang) – Steffen Ullrich Nov 14 '21 at 12:38
  • i ran it one time and then it prompts for a input.after giving inputs i hit enter again and then breaks.what am i missing here@SteffenUllrich – shouravRahman Nov 14 '21 at 12:38

1 Answers1

1

Do a little debugging by adding print lines in between your codes and see what's actually happening, you were just reading input the wrong way from command line

After Taking Reference from this link as posted above in comments by Steffen Ullrich

View In Go Playground

package main

import "fmt"

func main() {
    fmt.Println(`Enter the number of integers`)
    var n int
    if m, err := Scan(&n); m != 1 {
        panic(err)
    }
    fmt.Println(`Enter the integers`)
    inputSlice := make([]int, n)
    ReadN(inputSlice, 0, n)

    //Your Input Printing Out
    fmt.Println(inputSlice)

    //Calling Function
    BubbleSort(inputSlice)

    //Output
    fmt.Println(inputSlice)

}

func BubbleSort(input []int) {

    for i := 0; i < len(input)-1; i++ {
        for j := 0; j < len(input)-i-1; j++ {
            Swap(input, j)
        }
    }
}

func Swap(input []int, j int) {
    if input[j] > input[j+1] {
        input[j], input[j+1] = input[j+1], input[j]
    }
}

//Additional Functions
func ReadN(all []int, i, n int) {
    if n == 0 {
        return
    }
    if m, err := Scan(&all[i]); m != 1 {
        panic(err)
    }
    ReadN(all, i+1, n-1)
}

func Scan(a *int) (int, error) {
    return fmt.Scan(a)
}
MJK618
  • 161
  • 3
  • 5
  • 14