我正在實施排序,但在 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 }我在“if container[j] <= x”行中收到錯誤,提示panic:運行時錯誤:索引超出范圍 main.My_Partition(0x2101b20c0, 0x7, 0x7, 0x0, 0x6, ...)/Path/main.go:34 +0xff有人有想法嗎?我的交換功能在下面 func my_Swap(a *int, b *int) { temp := *a *a = *b *b = temp }但我不認為交換是問題所在。
1 回答

莫回無
TA貢獻1865條經驗 獲得超7個贊
你有一個錯字:
for j := first_index; i < last_index; j++ {
應該:
for j := first_index; j < last_index; j++ {
很容易犯的錯誤:-)
- 1 回答
- 0 關注
- 295 瀏覽
添加回答
舉報
0/150
提交
取消