python - 当函数在自身内部被调用时会发生什么(Python)
问题描述
在下面的示例中,在自身内部调用的函数内部究竟发生了什么?
在函数的哪个点输出什么?有没有类似于这里发生的事情可以用简单的方式重写?
def mergeSort(myList):
if len(myList) > 1:
mid = len(myList) // 2
left = myList[:mid]
right = myList[mid:]
# Recursive call on each half
mergeSort(left)
mergeSort(right)
# Two iterators for traversing the two halves
i = 0
j = 0
# Iterator for the main list
k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
# The value from the left half has been used
myList[k] = left[i]
# Move the iterator forward
i += 1
else:
myList[k] = right[j]
j += 1
# Move to the next slot
k += 1
# For all the remaining values
while i < len(left):
myList[k] = left[i]
i += 1
k += 1
while j < len(right):
myList[k]=right[j]
j += 1
k += 1
myList = [54,26,93,17,77,31,44,55,20]
mergeSort(myList)
print(myList)
解决方案
推荐阅读
- java - 如何在 FXML 中直接在图像下方对齐文本?
- android - 有没有办法在没有异步调用的情况下检查 firebase 集合是否为空?
- vba - 使用 VBA 从 Outlook 无法投递的电子邮件中检索电子邮件地址
- flutter - 如何在 Flutter App 中拥有一个全局单例对象
- r - 用 R 抓取 HTML 表格
- websocket - 如何使用机器人框架从 Websocket Connection 发送和接收响应
- javascript - 在 Spring Boot 中无法获取 cookie 值
- chef-infra - 厨师中的 ruby Mixlib::ShellOut 解析问题
- c# - 等待另一个任务 C# 更新对象
- html - 以更好的方式处理按钮的活动选择