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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?