气泡排序python中的外循环
气泡排序的规范Python实现看起来像这样:
array = [5,2,7,8,1,6,3]
n = len(array)
for i in range(n):
for j in range(n-i-1):
if array[j] > array[j+1]:
array[j],array[j+1] = array[j+1], array[j]
现在,我的理解是外圈的总数应为n-1,其中n是数组大小。但是,在上面的Python代码中,范围会产生序列0,1,2,3,4,5,6并通过循环进行7次迭代?我在这里想念什么?
The canonical python implementation of bubble sort looks like this:
array = [5,2,7,8,1,6,3]
n = len(array)
for i in range(n):
for j in range(n-i-1):
if array[j] > array[j+1]:
array[j],array[j+1] = array[j+1], array[j]
Now, my understanding is the total number of passes of the outer loop should be n-1 where n is the array size. However, in the python code above, range produces the sequence 0,1,2,3,4,5,6 and iterates through the loop 7 times? What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论