在合并排序的上一个递归过程中,有没有办法将返回值用作同一函数的参数]

问题描述 投票:0回答:1
我正在编码合并排序算法,但不知何故遇到了问题。问题是我需要将合并函数的返回值用作参数,作为同一合并函数的先前递归调用。抱歉,不清楚。

这是我的代码:

a=[10,5,2,20,-50,30] def mergeSort(arr): l=0 h=len(arr)-1 if h>l: mid=(l+h)//2 left=arr[l:mid+1] right=arr[mid+1:] mergeSort(left) mergeSort(right) merge(left,right) def merge(l,r): subarr=[] lc=0 rc=0 loop=True while loop: if lc>len(l)-1 and rc<=len(r)-1: for i in range(rc,len(r)): subarr.append(r[i]) loop=False elif lc<=len(l)-1 and rc>len(r)-1: for i in range(lc,len(l)): subarr.append(l[i]) loop=False elif l[lc]<r[rc]: subarr.append(l[lc]) lc+=1 loop=True elif r[rc]<l[lc]: subarr.append(r[rc]) rc+=1 loop=True elif l[lc]==r[rc]: subarr.append(l[lc]) subarr.append(r[rc]) lc+=1 rc+=1 loop=True

mergeSort(a)

任何帮助将不胜感激,谢谢:)

我正在编码合并排序算法,但不知何故遇到了问题。问题是我需要将合并函数的返回值用作自变量,作为......>

algorithm sorting recursion data-structures mergesort
1个回答
0
投票
首先,您需要实际return结果。现在,您什么也没返回,因此请返回None

第二,只分配给相同的变量。 left = mergeSort(left),依此类推。

© www.soinside.com 2019 - 2024. All rights reserved.