【问题标题】:Constructive Insertion Sort建设性插入排序
【发布时间】:2019-10-17 18:35:08
【问题描述】:

我在插入排序方面遇到了问题,我觉得我可能错过了排序的重点并误解了基本原理。

我们得到了一个插入排序,它编辑了输入它的数组。我们的任务是修改我们得到的代码,然后创建一个不会编辑原始数组的建设性插入排序

这是我目前的代码

def append(A, x):
    return A + [x] 

def insertionSort(A):
    B = []
    B = append(B, A[0])
    for i in range(1, len(A)):
        B = insert(B, A[i], i, A)
    return str(B)

def insert(B, k, hi, A):
    print(B, k, hi)
    for x in range(hi-1, -1, -1):
        if B[x] <= k:
            B = append(B, k)
            return B
        B = append(B, A[x])
    B[0] = k 
    return B

print(insertionSort([2,4,6,8,10,1,3,5,7,9]))

但是在列表中的第三个或第四个元素之后,它开始以相反的顺序将所有项目添加到列表的末尾


[2] 4 1
[2, 4] 6 2
[2, 4, 6] 8 3
[2, 4, 6, 8] 10 4
[2, 4, 6, 8, 10] 1 5
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2] 3 6
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2, 1, 10, 8, 6, 4, 3] 5 7
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2, 1, 10, 8, 6, 4, 3, 3, 1, 10, 8, 6, 5] 7 8
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2, 1, 10, 8, 6, 4, 3, 3, 1, 10, 8, 6, 5, 7] 9 9
[1, 4, 6, 8, 10, 10, 8, 6, 4, 2, 1, 10, 8, 6, 4, 3, 3, 1, 10, 8, 6, 5, 7, 9]

我无法理解为什么这是错误的。

非常感谢任何可以提供帮助的人。

【问题讨论】:

    标签: python sorting insertion-sort


    【解决方案1】:

    逆向问题出在insert函数中的for循环处 当您的循环达到这些值时,它会启动反向模式

    def insert(B, k, hi, A):
        # when hi=5
        for x in range(hi-1, -1, -1):
            # x = 4
            # here B[4] is 10 and k=1 so B[4] <= 1 is False
            # you program does not execute the inside of if
            # instead it jumps to B = append(B, A[x]) where A[4] == 10
            # and the this loop goes in reverse mode from 4 to 0
            # when x = 3
            # B[x] = 8 so 8 is not less or equal of k where k = 1
            # so it jumps again to B = append(B, A[x]) where A[x] = A[3] = 8
            # so it append 8 
            # and so on 
            # when this loop is finished your list will look like [1,4,6,8,10,10,8,6,4,2]
            # the 1 gets added when the loop is finished at B[0] = k 
            # and then rest of the outputs are result of the loop inside the insertionSort func
            if B[x] <= k:
                B = append(B, k)
                return B
            B = append(B, A[x])
        B[0] = k 
        return B
    

    这里有一个解决方案:

    def insertionSort(A):
        copy_sort = A.copy()
    
        for i in range(1, len(copy_sort)):
            item = copy_sort[i]
    
            j = i - 1
            while j >= 0 and copy_sort[j] > item:
                copy_sort[j + 1] = copy_sort[j]
                j -= 1
            copy_sort[j + 1] = item
    
        return copy_sort
    
    
    your_array = [2,4,6,8,10,1,3,5,7,9]
    sorted = insertionSort(your_array)
    print(your_array)
    print(sorted)
    

    【讨论】:

      【解决方案2】:

      您需要在纸上制定算法,然后将这些步骤转换为 Python 代码。您实现的内容令人费解且不正确。

      最重要的是,insert 对它需要的信息以及它应该如何工作感到非常困惑。从您的代码中我可以看出,您希望此例程将给定值k 插入列表B 中的适当位置。出于某种原因,您还传入了列表 A 以及该列表中的值的位置,这两者都不适用。

      然后你的例程做的很奇怪;从B 的末尾开始(使用i 而不是B 本身),代码检查B 的元素;每次在列表中找到一个小于新值的值时,它会将新值附加到B 的末尾。无论进行何种比较,它都会将A 的相应元素附加到B。 您没有将元素插入到适当的位置。

      重写这段代码。从最少的必要信息开始:​​

      def insert(arr, new_val):
      # insert new_val into the list arr
      

      现在,您的函数需要执行两个步骤:

      • new_val找到合适的位置
      • 使用插入该位置的值创建一个新列表。

      你返回那个新列表。

      你能从那里继续前进吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多