Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Go:数组超出索引死机错误_Arrays_Sorting_Indexing_Go - Fatal编程技术网

Arrays Go:数组超出索引死机错误

Arrays Go:数组超出索引死机错误,arrays,sorting,indexing,go,Arrays,Sorting,Indexing,Go,我正在实现排序,但在Go语言中不断出现索引界错误 我的代码如下 func My_Partition(container []int, first_index int, last_index int) int { var x int = container[last_index] i := first_index - 1 for j := first_index; i < last_index; j++ { if containe

我正在实现排序,但在Go语言中不断出现索引界错误

我的代码如下

 func My_Partition(container []int, first_index int, last_index int) int {
      var x int = container[last_index]
      i := first_index - 1

      for j := first_index; i < last_index; j++ {
           if container[j] <= x {
                i += 1
                my_Swap(&container[i], &container[j])
           }
      }
      my_Swap(&container[i+1], &container[last_index])
      return i+1
 }
有人有主意吗

我的交换功能如下

 func my_Swap(a *int, b *int) {
      temp := *a
      *a = *b
      *b = temp
 }
但我不认为交换是问题。

您有一个输入错误:

for j := first_index; i < last_index; j++ {
j:=第一个索引的
;i
应该是:

for j := first_index; j < last_index; j++ {
j:=第一个索引;j<最后一个索引;j++{
容易犯错误:-)

您有一个输入错误:

for j := first_index; i < last_index; j++ {
j:=第一个索引;i<最后一个索引;j++{ 应该是:

for j := first_index; j < last_index; j++ {
j:=第一个索引;j<最后一个索引;j++{ 容易犯错误:-)


非常感谢。这是一个愚蠢的错误,三个小时都无法解决……谢谢!@dfsadxsadqwd213没问题,我已经做过很多次类似的事情了!为了将来参考,您不需要自定义交换函数。Go支持多个赋值,所以您可以只执行
container[I],container[j]=container[j],container[I]
不需要临时变量。这很好。我当时是用C语言编写的。我应该像在Ruby和Python中那样尝试。ThanksI几年前犯了一个类似的错误,现在我尽量避免使用单字母变量。如果没有这些错误,额外的键入是一个很小的代价。非常感谢。那是一个愚蠢的错误花了三个小时,我都想不出来了……谢谢!@dfsadxsadqwd213没问题,我已经做过很多次类似的事情了!为了以后的参考,你不需要自定义交换函数。Go支持多个赋值,所以你可以只做
container[I],container[j]=container[j],container[I]
不需要临时变量。这很好。我当时是用C语言编写的。我应该像在Ruby和Python中那样尝试。ThanksI几年前也犯过类似的错误,现在我尽量避免使用单字母变量。如果没有这些bug,额外的键入是一个很小的代价。