在Python中尝试实现一个函数式递归合并排序已经有好几天了。除此之外,我希望能够打印出排序算法的每个步骤。有没有办法让这段Python代码以函数范式的方式运行?这就是我目前所知道的..。
def merge_sort(arr, low, high):
# If low is less than high, proceed. Else, array is sorted
if low < high:
mid = (low + high) // 2 # Get the midpoint
merge_sort (arr, low, mid) # Recursively split the left half
merge_sort (arr, mid+1, high) # Recursively split the right half
return merge(arr, low, mid, high) # merge halves together
def merge (arr, low, mid, high):
temp = []
# Copy all the values into a temporary array for displaying
for index, elem in enumerate(arr):
temp.append(elem)
left_p, right_p, i = low, mid+1, low
# While left and right pointer still have elements to read
while left_p <= mid and right_p <= high:
if temp[left_p] <= temp[right_p]: # If the left value is less than the right value. Shift left pointer by 1 unit to the right
arr[i] = temp[left_p]
left_p += 1
else: # Else, place the right value into target array. Shift right pointer by 1 unit to the right
arr[i] = temp[right_p]
right_p += 1
i += 1 # Increment target array pointer
# Copy the rest of the left side of the array into the target array
while left_p <= mid:
arr[i] = temp[left_p]
i += 1
left_p += 1
print(*arr) # Display the current form of the array
return arr
def main():
# Get input from user
arr = [int(input()) for x in range(int(input("Input the number of elements: ")))]
print("Sorting...")
sorted_arr = merge_sort(arr.copy(), 0, len(arr)-1)
print("\nSorted Array")
print(*sorted_arr)
if __name__ == "__main__":
main()任何帮助都将不胜感激!谢谢。
发布于 2021-01-21 20:19:58
在纯函数式的合并排序中,我们不想改变任何值。
我们可以像这样定义一个很好的零突变的递归版本:
def merge(a1, a2):
if len(a1) == 0:
return a2
if len(a2) == 0:
return a1
if a1[0] <= a2[0]:
rec = merge(a1[1:], a2)
return [a1[0]] + rec
rec = merge(a1, a2[1:])
return [a2[0]] + rec
def merge_sort(arr):
if len(arr) <= 1:
return arr
halfway = len(arr) // 2
left = merge_sort(arr[:halfway])
right = merge_sort(arr[halfway:])
return merge(left, right)您可以在merge_sort的顶部添加一个print(arr)来逐步打印,但是技术上的副作用会使它变得不纯(尽管在这种情况下仍然是引用透明的)。然而,在python中,您不能使用monad将副作用从纯计算中分离出来,因此,如果您希望真正避免这种打印,则必须返回层,并在最后打印它们:)
此外,从技术上讲,这个版本做了很多列表的副本,所以它相对较慢。这可以通过使用链表,并使用/取消使用它来修复。然而,这超出了范围。
https://stackoverflow.com/questions/65826075
复制相似问题