如何对数组进行排序

我在 Go 中对数组进行排序时遇到问题,这是我的代码:


func main() {

    fmt.Print("Masukkan Jumlah Data yang akan dimasukkan: ")

    var jumlahdata int

    fmt.Scanln(&jumlahdata)

    var DataDiagram = make([]int, jumlahdata)

    fmt.Print("Masukkan data secara berurutan dengan spasi sebagai pemisah antar angka: ")

    for i := 0; i < jumlahdata; i++ {

        fmt.Scanf("%d", &DataDiagram[i])

    }

    fmt.Print("\n")

    var max int = DataDiagram[0]

    for _, value := range DataDiagram { // Menemukan nilai maximum

        if value > max {

            max = value

        }

    }

    var mem int

    Sorting(DataDiagram, jumlahdata, mem, max)

}

func Grafik(jumlahdata int, max int, DataDiagram []int) {

    for i := max; i >= 1; i-- { // membuat Data Diagram

        for j := 0; j < jumlahdata; j++ {

            if DataDiagram[j] >= i {

                fmt.Print(" | ")

            } else {

                fmt.Print("   ")

            }

        }

        fmt.Print("\n")

    }

    for i := 0; i < jumlahdata; i++ {

        fmt.Print("---")

    }

    fmt.Print("\n")

    fmt.Print(" ")

    for i := 0; i < jumlahdata; i++ {

        fmt.Print(DataDiagram[i], "  ")

    }

}

func Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {

    for langkah := 0; langkah < (jumlahdata-1) ; langkah++ {

        Grafik(jumlahdata, max, DataDiagram)

        for i := 0; i < jumlahdata - (langkah-1); i++ {

            if DataDiagram[i] > DataDiagram[i + 1] {

                mem := DataDiagram[i];

                DataDiagram[i] = DataDiagram[i + 1]

                DataDiagram[i + 1] = mem;

            }

        }

    }

}

我期待的是这样的: 我期待什么

但是输出另有说明,它给了我错误:它给了我错误

有人可以提供一些指导如何解决这个问题:) 我昨天刚学 Go,它类似于 C,但一直给我 index out of range 错误


茅侃侃
浏览 123回答 2
2回答

天涯尽头无女友

我了解您的任务是对int“数组”(slice,通俗地说)进行排序,将您工作的每个步骤显示为图表。因为你必须展示你的工作,所以你不能使用 go 的内置排序,例如sort.Ints(DataDiagram).您的问题与Sorting功能有关。第 1 步您直接导致崩溃的问题是i最终迭代到大于上索引的数字DataDiagram。我们在下面的注释行中修复。// Step 1: fix the iteratorfunc Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {&nbsp; &nbsp; for langkah := 0; langkah < (jumlahdata-1) ; langkah++ {&nbsp; &nbsp; &nbsp; &nbsp; Grafik(jumlahdata, max, DataDiagram)&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < jumlahdata - 1; i++ { // Was: for i := 0; i < jumlahdata - (langkah-1); i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if DataDiagram[i] > DataDiagram[i + 1] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mem := DataDiagram[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataDiagram[i] = DataDiagram[i + 1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataDiagram[i + 1] = mem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}步骤 2代码不再崩溃,但不能保证排序,因为它只通过输入一次。我们需要继续循环,直到不再发生交换。这个问题在下面得到解决。代码现在在操场上产生预期的输出。// Step 2: loop until sortedfunc Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {&nbsp; &nbsp; swapped := true&nbsp; &nbsp; for swapped {&nbsp; &nbsp; &nbsp; &nbsp; Grafik(jumlahdata, max, DataDiagram)&nbsp; &nbsp; &nbsp; &nbsp; swapped = false&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < jumlahdata - 1; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if DataDiagram[i] > DataDiagram[i + 1] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mem := DataDiagram[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataDiagram[i] = DataDiagram[i + 1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataDiagram[i + 1] = mem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swapped = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}第 3 步上面的代码工作正常,但也许可以使用一些整理。最终结果在操场上没有改变。// Step 3: make it prettierfunc Sorting(data []int) {&nbsp; &nbsp; max := data[0]&nbsp; &nbsp; for _, value := range data { // Menemukan nilai maximum&nbsp; &nbsp; &nbsp; &nbsp; if value > max {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = value&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; swapped := true&nbsp; &nbsp; for swapped {&nbsp; &nbsp; &nbsp; &nbsp; Grafik(len(data), max, data)&nbsp; &nbsp; &nbsp; &nbsp; swapped = false&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < len(data)-1; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if data[i] > data[i+1] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[i], data[i+1] = data[i+1], data[i]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swapped = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

呼啦一阵风

如果您只使用 sort.Ints(ints) 会简单得多,您可以在此处看到:&nbsp;https ://goplay.space/#i9VIrDG-vL-
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go