dpq39825 2013-08-31 06:30
浏览 44
已采纳

执行:数组超出索引错误

I am implementing sorting but keep getting the index bound error in Go language.

My code is following

 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
 }

I am getting error in the line "if container[j] <= x" that says panic: runtime error: index out of range

    main.My_Partition(0x2101b20c0, 0x7, 0x7, 0x0, 0x6, ...)
/Path/main.go:34 +0xff

Anybody has an idea?

my swap function is below

 func my_Swap(a *int, b *int) {
      temp := *a
      *a = *b
      *b = temp
 }

but I don't think swap is the problem.

  • 写回答

1条回答 默认 最新

  • douao1854 2013-08-31 07:08
    关注

    You have a typo:

    for j := first_index; i < last_index; j++ {
    

    Should be:

    for j := first_index; j < last_index; j++ {
    

    Easy enough mistake to make :-)

    Playground example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀