Home > Mobile >  How to sort array in Go Language?
How to sort array in Go Language?

Time:10-28

So i have problem for sorting my array in Go Lang, Here is my code :

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

What i expect is look like this: What I Expect

But the output said otherwise, it give me error : It give me error

Can someone give some guide how to fix this :) i just learn Go yesterday, it similiar to C, but keep giving me index out of range error

CodePudding user response:

I understand your task is to sort an int "array" (slice, in go-speak), showing each step of your work as a graph. Because you must show your work, you can't use go's built-in sorting, e.g. sort.Ints(DataDiagram).

Your problems are with the Sorting function.

Step 1 Your immediate crash-causing problem is that i eventually iterates to a number larger than upper index of DataDiagram. That we fix in the commented line below.

// Step 1: fix the iterator
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 - 1; i   { // Was: 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;
            }
        }
    }
}

Step 2 The code no longer crashes, but is not guaranteed to sort, because it makes only one pass through the inputs. We need to continue looping until there's no more swapping taking place. That problem is fixed below. The code now produces the expected output on the playground.

// Step 2: loop until sorted
func Sorting(DataDiagram []int, jumlahdata int, mem int, max int) {
    swapped := true

    for swapped {
        Grafik(jumlahdata, max, DataDiagram)
        swapped = false
        for i := 0; i < jumlahdata - 1; i   {
            if DataDiagram[i] > DataDiagram[i   1] {
                mem := DataDiagram[i];
                DataDiagram[i] = DataDiagram[i   1]
                DataDiagram[i   1] = mem;
                swapped = true
            }
        }
    }
}

Step 3 The above code works fine, but perhaps can use some tidying. The end result is unchanged on the playground.

// Step 3: make it prettier
func Sorting(data []int) {

    max := data[0]

    for _, value := range data { // Menemukan nilai maximum
        if value > max {
            max = value
        }
    }

    swapped := true

    for swapped {
        Grafik(len(data), max, data)
        swapped = false
        for i := 0; i < len(data)-1; i   {
            if data[i] > data[i 1] {
                data[i], data[i 1] = data[i 1], data[i]
                swapped = true
            }
        }
    }
}
  • Related