【发布时间】:2021-07-25 00:44:53
【问题描述】:
我向自己提出了一个挑战,即在不查找代码示例的情况下重新创建插入排序,仅来自 " pop Number out 数组,与同一索引点的数字比较,如果Number小于数组索引点的数字,则向下移动数组负1,当数组索引点的值小于Number时,插入到那个时候,” 我已经将这件事重写了 4 次,我宁愿有方向,而不仅仅是在谷歌上搜索答案,
我正在自学编码,我喜欢限制自己,因为我相信它会更加自律
他是代码, 它似乎适用于 while 循环的 1 次迭代,但不会更进一步,
array = [3,2,1,0] 得到 [2, 1, 0, 3]
testArray = [3,2,1,0]
index = 0
for n in testArray:
tempIndex = index - 1
if index > 0:
if n < testArray[tempIndex]:
testArray.pop(index)
while True:
if testArray[tempIndex] < n:
tempIndex -= 1
else:
break
testArray.insert(tempIndex,n)
index += 1
编辑:删除调试器以压缩代码
【问题讨论】:
-
“我做了一个调试器来看看发生了什么”:和?你发现了什么?把你的代码丢在这里,指望别人帮你调试是不行的。请使用tour,阅读How to Ask 和question checklist。进行一些调试,然后将您的代码压缩为一个minimal reproducible example,该minimal reproducible example 重现了您所询问的特定 问题。欢迎使用 Stack Overflow!
-
正如我在帖子中解释的那样,代码在 while 循环中迭代一次,这是调试器告诉我的。所以我真的不明白你为什么提出我做了一个调试器,而事实上我告诉你它做了什么,我确实做了检查清单,我想我可能应该删除我正在调用的调试器,我将在未来,
标签: python arrays sorting insertion