使用Go语言实现冒泡排序。

huangapple go评论93阅读模式
英文:

implementing bubble sort with go

问题

请帮我实现冒泡排序。如果我在main()函数中使用硬编码的切片调用它,它可以正常工作,但如果我使用Scan函数从动态输入调用它,它就会出错。

以下是我目前的代码:

package main

import "fmt"

func main() {
	fmt.Println("请输入最多10个数字:")

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

终端输出:

coder:~/project$ go run bubblesort.go
请输入最多10个数字:
12 24 54 65 11
coder:~/project$ 4 54 65 11
bash: 4: command not found
英文:

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 &quot;fmt&quot;

func main() {
	fmt.Println(&quot;Enter a maximum of 10 numbers: &quot;)

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

	BubbleSort(inputSlice)

	fmt.Println(inputSlice)

}

func BubbleSort(input []int) {

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

func Swap(input []int, j int) {
	if input[j] &gt; 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

答案1

得分: 1

在你的代码中添加打印语句进行一些调试,看看实际发生了什么。你只是从命令行错误地读取了输入。

参考了上面评论中这个链接,由Steffen Ullrich发布。

在Go Playground中查看

package main

import "fmt"

func main() {
    fmt.Println(`输入整数的个数`)
    var n int
    if m, err := Scan(&n); m != 1 {
        panic(err)
    }
    fmt.Println(`输入整数`)
    inputSlice := make([]int, n)
    ReadN(inputSlice, 0, n)

    // 打印输入
    fmt.Println(inputSlice)

    // 调用函数
    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]
    }
}

// 附加函数
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)
}
英文:

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 &quot;fmt&quot;
func main() {
fmt.Println(`Enter the number of integers`)
var n int
if m, err := Scan(&amp;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 &lt; len(input)-1; i++ {
for j := 0; j &lt; len(input)-i-1; j++ {
Swap(input, j)
}
}
}
func Swap(input []int, j int) {
if input[j] &gt; 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(&amp;all[i]); m != 1 {
panic(err)
}
ReadN(all, i+1, n-1)
}
func Scan(a *int) (int, error) {
return fmt.Scan(a)
}

huangapple
  • 本文由 发表于 2021年11月14日 20:14:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/69962858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定