简体   繁体   English

python的合并排序

[英]Merge sort for python

Here are my merge and mergeSort functions.这是我的mergemerge mergeSort功能。 merge merges two separate arrays and mergesSort sorts and merges them with recursion: merge合并两个单独的数组并mergesSort排序并使用递归合并它们:

def merge(arrL, arrR):
    arrNew = []
    d = len(arrL) + len(arrR)
    i = 0
    j = 0
    for k in range (0, d-1):
        if (arrL[i] < arrR[j]) :
            arrNew.append(arrL[i]) # appends to the end of the array
            i = i + 1
        else:
            arrNew.append(arrR[j])
            j = j + 1
    return arrNew

def mergeSort(arr, m, n):
    if (n - m == 1):
        return arr[m]
    else:
        p = (m + n) // 2
        arrL = mergeSort(arr, m, p)
        arrR = mergeSort(arr, p, n)
        arrNew = merge(arrL, arrR)
        return arrNew

I am getting an error from lines 32, 33 and 13:我在第 32、33 和 13 行收到错误消息:

d = len(arrL) + len(arrR)
TypeError: object of type 'int' has no len()

What is causing this error?是什么导致了这个错误? merge is taking two arrays as inputs.合并将两个数组作为输入。

What is causing this error?是什么导致了这个错误? merge is taking two arrays as inputs. merge将两个数组作为输入。

Except when it doesn't.除非它没有。

if(n-m == 1):
  return arr[m]

This output of mergeSort is not an array. mergeSort这个输出不是一个数组。

My guess is it's this line我猜是这条线

if(n-m == 1):
    return arr[m]

which presumably is returning the content arr[m] of the array and not an array itself.这大概是返回数组的内容arr[m]而不是数组本身。

Since your code sorts arrays, when this naked element gets recursed on, it will generate the error you're seeing.由于您的代码对数组进行排序,当这个裸元素被递归时,它会产生您看到的错误。

There are multiple problems in the code:代码中存在多个问题:

  • in mergeSort , you should return arr instead of arr[m] when the length of the array is less than 2. The test if (n - m == 1) does not allow for empty arrays:mergeSort ,当数组的长度小于 2 时,您应该返回arr而不是arr[m] 。测试if (n - m == 1)不允许空数组:

     if (n - m < 2): return arr
  • in merge , the main loop should run d times, ie: instead of for k in range (0, d-1): you should write:merge ,主循环应该运行d次,即:而不是for k in range (0, d-1):你应该写:

     for k in range (d):
  • the test in the merge loop should also check if the index value in still in range.合并循环中的测试还应检查索引值是否仍在范围内。 If the second slice is exhausted, the element arrL[i] should be selected:如果第二个切片用完,则应选择元素arrL[i]

     for k in range (d): if i < len(arrL) and (j >= len(arrR) or arrL[i] < arrR[j]): arrNew.append(arrL[i]) i = i + 1 else: arrNew.append(arrR[j]) j = j + 1

Here is a modified version:这是一个修改后的版本:

def merge(arrL, arrR):
    arrNew = []
    i = 0
    j = 0
    for k in range(len(arrL) + len(arrR)):
        if i < len(arrL) and (j >= len(arrR) or arrL[i] < arrR[j]):
            arrNew.append(arrL[i])
            i = i + 1
        else:
            arrNew.append(arrR[j])
            j = j + 1
    return arrNew

def mergeSort(arr, m, n):
    if (n - m < 2):
        return arr
    else:
        p = (m + n) // 2
        arrL = mergeSort(arr, m, p)
        arrR = mergeSort(arr, p, n)
        return merge(arrL, arrR)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM