【问题标题】:Python: Insertion sort logic is failing but i'm not sure why [closed]Python:插入排序逻辑失败,但我不知道为什么[关闭]
【发布时间】: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 Askquestion checklist。进行一些调试,然后将您的代码压缩为一个minimal reproducible example,该minimal reproducible example 重现了您所询问的特定 问题。欢迎使用 Stack Overflow!
  • 正如我在帖子中解释的那样,代码在 while 循环中迭代一次,这是调试器告诉我的。所以我真的不明白你为什么提出我做了一个调试器,而事实上我告诉你它做了什么,我确实做了检查清单,我想我可能应该删除我正在调用的调试器,我将在未来,

标签: python arrays sorting insertion


【解决方案1】:

我认为您没有正确回溯,这就是您面临这种问题的原因。但是我解决了它。正确查看。

testArray = [3,2,1,0]
index = 0
for n in testArray:
    tempIndex = index - 1
    
    print("index:     ",index)
    print("tempIndex: ",tempIndex)
    if index > 0:
        if n < testArray[tempIndex]:
                testArray.pop(index)
                while True:
                    print("While loop: ",testArray[tempIndex])
                    if testArray[tempIndex] < n:
                        tempIndex -= 1
                    else:
                        break
                testArray.insert(tempIndex,n)
                print("TestArray:-----> ",testArray)
                count=tempIndex
                print("count: ",count)
                while(testArray[count]<testArray[count-1]):
                  #Swap value till the start of the testArray[0]
                  temp = testArray[count]
                  testArray[count]= testArray[count-1]
                  testArray[count-1] = temp
                  print("TestArray:---swap->  ",testArray)
                  if(count != 1):
                    count = count-1
                print("TestArray: ",testArray)
    index += 1

大量使用打印语句来检查您的逻辑(思维)是否正常工作,代码是否相应地工作:

index:      0
tempIndex:  -1
index:      1
tempIndex:  0
While loop:  3
TestArray:----->  [2, 3, 1, 0]
count:  0
TestArray:  [2, 3, 1, 0]
index:      2
tempIndex:  1
While loop:  3
TestArray:----->  [2, 1, 3, 0]
count:  1
TestArray:---swap->   [1, 2, 3, 0]
TestArray:  [1, 2, 3, 0]
index:      3
tempIndex:  2
While loop:  3
TestArray:----->  [1, 2, 0, 3]
count:  2
TestArray:---swap->   [1, 0, 2, 3]
TestArray:---swap->   [0, 1, 2, 3]
TestArray:  [0, 1, 2, 3]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2014-04-08
    • 2020-01-04
    相关资源
    最近更新 更多