【发布时间】:2015-05-12 10:44:18
【问题描述】:
你好我想问一下为什么当我在arr下面运行这段代码时没有改变它的值
def merge(arr, left, middle, right):
newArr = []
leftCounter = 0
rightCounter = middle + 1
while leftCounter <= middle and rightCounter <= right:
if arr[leftCounter] < arr[rightCounter]:
newArr.append(arr[leftCounter])
leftCounter += 1
else:
newArr.append(arr[rightCounter])
rightCounter += 1
while leftCounter <= middle:
newArr.append(arr[leftCounter])
leftCounter += 1
while rightCounter <= right:
newArrCounter.append(arr[rightCounter])
rightCounter += 1
arr = newArr
def main():
arr = [1, 5, 7, 2, 4, 6]
merge(arr, 0, 2, 5)
print(arr)
这个结果是 [1, 5, 7, 2, 4, 6]
谢谢
【问题讨论】:
-
为了记录,这个函数是标准库的一部分,如
heapq.merge()。