Python 2.7 在python中使用合并排序来排列给定的整数列表以获得最少的整数

Python 2.7 在python中使用合并排序来排列给定的整数列表以获得最少的整数,python-2.7,sorting,merge,Python 2.7,Sorting,Merge,假设我的给定数组是[9,891000577987],我在合并排序算法中使用了自定义比较。我想得到[1000577987,89,9],它可以在排列数字后用于获得最小的数字1000577987899。我使用random.randint(0100)生成一个数组。 我的输入:[70,73,85,60,76] 我的输出: before: [70, 73, 85, 60, 76] ['70', '73', '85', '60', '76'] splitting the array ['70', '73',

假设我的给定数组是[9,891000577987],我在合并排序算法中使用了自定义比较。我想得到[1000577987,89,9],它可以在排列数字后用于获得最小的数字1000577987899。我使用random.randint(0100)生成一个数组。 我的输入:
[70,73,85,60,76]
我的输出:

before: [70, 73, 85, 60, 76]
['70', '73', '85', '60', '76']
splitting the array ['70', '73', '85', '60', '76']
splitting the array ['70', '73']
merging ['70', '73']
splitting the array ['85', '60', '76']
splitting the array ['60', '76']
merging ['60', '76']
Traceback (most recent call last):
File "venture.py", line 66, in <module>
mergeSort(b)
  File "venture.py", line 33, in mergeSort   
mergeSort(righthalf) #the recursive call after splitting
  File "venture.py", line 41, in mergeSort
if  int(lefthalf[i])*10**len(righthalf[j])+int(righthalf[j]) < int(righthalf[j])*10**len(lefthalf[i])+int(lefthalf[j]): #custom_comparison
IndexError: list index out of range
之前:[70,73,85,60,76]
['70', '73', '85', '60', '76']
拆分数组['70'、'73'、'85'、'60'、'76']
拆分数组['70','73']
合并['70','73']
拆分数组['85'、'60'、'76']
拆分数组['60','76']
合并['60','76']
回溯(最近一次呼叫最后一次):
文件“venture.py”,第66行,在
合并排序(b)
文件“venture.py”,第33行,合并排序
mergeSort(rightshalf)#拆分后的递归调用
文件“venture.py”,第41行,合并排序
如果int(lefthalf[i])*10**len(righhalf[j])+int(righhalf[j])
我的代码:

import random

def mergeSort(alist):
    if len(alist)>1:
        print "splitting the array",alist
        mid = len(alist)//2  #the floor div
        lefthalf = alist[:mid]
        righthalf = alist[mid:]
        mergeSort(lefthalf)  #the recursive call after splitting
        mergeSort(righthalf) #the recursive call after splitting

        i=0
        j=0
        k=0


        while i < len(lefthalf) and j < len(righthalf):
            if  int(lefthalf[i])*10**len(righthalf[j])+int(righthalf[j]) < int(righthalf[j])*10**len(lefthalf[i])+int(lefthalf[j]): #custom comparison
                alist[k]=lefthalf[i]
                i=i+1
            else:
                alist[k]=righthalf[j]
                j=j+1
            k=k+1

        while i < len(lefthalf):  #to be used when index j is out of range of len(righthalf)
            alist[k]=lefthalf[i]
            i=i+1
            k=k+1

        while j < len(righthalf): #to be used when index i is out of range of len(lefthalf)
            alist[k]=righthalf[j]
            j=j+1
            k=k+1
        print "merging",alist



a = [random.randint(0,100) for c in range(5)]
print "before:",a
b = map(str, a)
print b
mergeSort(b)
print "new :",b
随机导入
def合并排序(列表):
如果len(alist)>1:
打印“拆分数组”,如有必要
mid=len(alist)//2#地板分区
lefthalf=alist[:mid]
righthalf=alist[中间:]
mergeSort(lefthalf)#拆分后的递归调用
mergeSort(rightshalf)#拆分后的递归调用
i=0
j=0
k=0
而i
为什么会发生错误相同的代码适用于常规比较
如果lefthalf[i]
。我刚刚将如果条件更改为
如果int(lefthalf[i])*10**len len righhalf[j]+int(righhalf[j])

我错在哪里?

请记住,如果原始列表(
alist
)是奇数,则
右半部分和
左半部分的长度将不同

您的while条件是
j
,但您的if条件使用
lefthalf[j]


alist
是奇数且
len(alist)=2N+1
时,如果
j
上升到
len(rightshalf)-1=(N+1)-1=N
,你会得到一个索引超出范围的错误,因为
lefthalf[N]
超出范围。

再次感谢ali,它现在工作正常。我忘了正确检查索引。