仅使用一个辅助数组来实现合并排序(而不是递归地创建新数组)

时间:2019-06-05 17:31:42

标签: python python-3.x algorithm python-2.7 mergesort

实现合并排序的基本方法随处可见,是每次执行拆分时我们递归创建新的左右数组的方法-

https://www.geeksforgeeks.org/merge-sort/-> [1]

我只想创建一个辅助数组,就像在下面的链接中所做的那样-

https://www.coursera.org/learn/algorithms-part1/lecture/ARWDq/mergesort-> [2]-分钟(7-10)

讲师清楚地在视频中的9:30声明-  重要的是不要在递归例程中创建辅助数组,因为这可能会导致额外的数组创建成本。而且有时您会看到Mergesort由于该错误而表现不佳。

它不会递归创建新数组。

基本上,我想用python编写Coursera链接中提到的代码

这是我到目前为止写的->

class merge_sort:

    def __init__(self, data):
        self.data = data

    aux_array = []


    def merge(array, aux_array, low, mid, high):

        for k in range(low, high):
            aux_array[k] = self.data[k]

        i = low
        j = mid + 1
        for k in range(low, high):
            if(i > mid):
                self.data[k] = aux_array[i]
                i = i +1
            elif(j > high):
                self.data[k] = aux_array[j]
                j = j +1
            elif(aux_array[i] < aux_array[j]):
                self.data[k] = aux_array[i]
                i = i +1
            else:
                self.data[k] = aux_array[j]
                j = j +1


    def mergesort(self, data, low, high):
        #high = len(data)
        mid = (high - low)//2
        mergesort(data, low, mid)
        mergesort(data, mid+1, high)
        merge(data, aux_array, low, mid, high)


    def start_sort(self):
        high = len(self.data) - 1
        self.mergesort(self.data, 0, high)


arr = [10,2,30,0,4]

arr1 = merge_sort(arr)
arr1.start_sort()
print(arr1)

我当前遇到以下错误-

TypeError: mergesort() takes 3 positional arguments but 4 were given

我也尝试过这样做-

@classmethod
    def mergesort(cls, data, low, high):
        #high = len(data)
        mid = (high - low)//2
        mergesort(data, low, mid)
        mergesort(data, mid+1, high)
        merge(data, aux_array, low, mid, high)

    def start_sort(self):
        high = len(self.data) - 1
        self.mergesort(self.data, 0, high)

在这种情况下,出现以下错误-

NameError: name 'mergesort' is not defined

2 个答案:

答案 0 :(得分:0)

签入您在其中调用mergesort(...)的合并排序方法 你应该打电话给self.mergesort(...)

这将解决名称未定义的错误,并且会为您带来递归深度错误,因为您在没有退出条件的情况下递归调用mergesort(尽管我认为下一步要进行此操作)

祝您好运:)

我做参考的编辑

    def mergesort(self, data, low, high):
        #high = len(data)
        mid = (high - low)//2
        self.mergesort(data, low, mid)      # <- 
        self.mergesort(data, mid+1, high)   # <-
        merge(data, aux_array, low, mid, high) 

答案 1 :(得分:0)

我只有python 2.7。我没有上课。注释中指出的修复程序。

def merge(array, aux_array, low, mid, high):
    for k in range(low, high+1):                # fix (high+1)
        aux_array[k] = array[k]                 # fix (array)
    i = low
    j = mid + 1
    for k in range(low, high+1):                # fix (high+1)
        if(i > mid):
            array[k] = aux_array[j]             # fix (j)
            j = j +1                            # fix (j)
        elif(j > high):
            array[k] = aux_array[i]             # fix (i)
            i = i +1                            # fix (i)
        elif(aux_array[i] <= aux_array[j]):     # fix (<=)
            array[k] = aux_array[i]
            i = i +1
        else:
            array[k] = aux_array[j]
            j = j +1

def mergesort(array, aux_array, low, high):     # fix (names)
    if(low >= high):                            # fix (size < 2)
        return                                  # fix (size < 2)
    mid = low + ((high - low)//2)               # fix (low +)
    mergesort(array, aux_array, low, mid)       # fix (names)
    mergesort(array, aux_array, mid+1, high)    # fix (names)
    merge(array, aux_array, low, mid, high)     # fix (names)

def start_sort(array):                          # fix (names)
    aux_array = [0] * len(array)                # fix allocate aux_array
    mergesort(array, aux_array, 0, len(array)-1)

arr = [10,2,30,0,4]

start_sort(arr)                                 # fix
print(arr)                                      # fix