关于在Golang中对二维数组进行排序的问题

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

Question about sorting 2 dimensional array in Golang

问题

我对Go语言中的二维数组有一个问题。我不知道为什么我使用的排序方法不起作用,但是在Java、C和C++中它运行得很好,但是当我在Go语言中应用相同的排序方法时,我不确定出了什么问题,有时它给我正确的结果,有时候它根本不排序。请帮忙看看。非常感谢。

package main

import (
   "fmt"
)

var employee int = 0
var day int = 1

func main() {

   list := [][]int{{2, 4, 3, 4, 5, 8, 8},
                   {7, 3, 4, 3, 3, 4, 4},
                   {3, 3, 4, 3, 3, 2, 2},
                   {9, 3, 4, 7, 3, 4, 1},
                   {3, 5, 4, 3, 6, 3, 8},
                   {3, 4, 4, 6, 3, 4, 4},
                   {3, 7, 4, 8, 3, 8, 4},
                   {6, 3, 5, 9, 2, 7, 9}}

   var result [8][2]int 

   for i := 0; i < len(result); i++ {

       var total int = 0

       for j := 0; j < len(list[i]); j++ {
          total += list[i][j]
       }

       result[i][employee] = i 
       result[i][day] = total
   }

   sort(result)

   fmt.Println("The decreasing order is:")

   for i := 0; i < len(result); i++ {

       fmt.Printf("Employee %v's total dayoff is %v\n", result[i][employee], result[i][day])
    }
}

func sort(list [8][2]int) {

    for i := 0; i < len(list); i++ {

        var max_day int = list[i][day]
        var max_employee int = list[i][employee]
        var max_index int = i 

        for j := i + 1; j < len(list); j++ {

            if list[j][day] > max_day {
                max_day = list[j][day]
                max_employee = list[j][employee]
                max_index = j
            }
        }

        if max_index != i {
            list[max_index][employee] = list[i][employee]
            list[max_index][day] = list[i][day]
            list[i][employee] = max_employee
            list[i][day] = max_day
        }
    }
}
英文:

I have a question about 2 dimensional array in GoLang. I do not know why the sort method I use does not work, but It works well in java, C, and C++, but I am not sure what is wrong when I applied the same sort method in Golang, sometimes it give me the right result, sometimes, it does not sort at all. Please help. Thank you so much in advance.

package main
import (
&quot;fmt&quot;
)
var employee int = 0
var day int = 1
func main() {
list := [][] int {{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}}
var result [8][2] int 
for i := 0; i &lt; len(result); i++ {
var total int = 0
for j := 0; j &lt; len(list[i]); j++ {
total += list[i][j]
}
result[i][employee] = i 
result[i][day] = total
}
sort(result)
fmt.Println(&quot;The decreasing order is:&quot;)
for i := 0; i &lt; len(result); i++ {
fmt.Printf(&quot;Employee %v&#39;s total dayoff is %v\n&quot;, result[i][employee], result[i][day])
}
}
func sort(list[8][2] int) {
for i := 0; i &lt; len(list); i++ {
var max_day int = list[i][day]
var max_employee int = list[i][employee]
var max_index int = i 
for j := i + 1; j &lt; len(list); j++ {
if list[j][day] &gt; max_day {
max_day = list[j][day]
max_employee = list[j][employee]
max_index = j
}
}
if max_index != i {
list[max_index][employee] = list[i][employee]
list[max_index][day] = list[i][day]
list[i][employee] = max_employee
list[i][day] = max_day
}
}
}

答案1

得分: 3

你正在修改 sort() 函数中的 list 的副本,请将其原型更改为:

func sort(list *[8][2]int) {

并调用它:

sort(&result)
英文:

You're modifying a copy of list in your sort() function, change its prototype to:

func sort(list *[8][2]int) {

and call it:

sort(&amp;result)

huangapple
  • 本文由 发表于 2022年2月26日 01:09:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/71269333.html
匿名

发表评论

匿名网友

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

确定