簡體   English   中英

需要幫助使用 pygame 實現合並排序的可視化工具

[英]Need help implementing a visualizer for merge sort with pygame

我目前正在使用 pygame 構建一個排序可視化器,到目前為止我已經能夠進行冒泡和選擇排序。 我目前正在做的是每次進行更改時繪制數組的框架,這適用於氣泡和選擇排序。 但是對於合並排序,arrays 總是被拆分並合並回來,所以我不能每幀都繪制數組,因為每幀只會顯示數組的一部分。 我目前被困在如何在合並排序上實現可視化器,下面我將舉一個例子說明什么適用於冒泡排序,以及我目前有什么適用於合並排序。

# Bubble sort
def bubble_sort_visual(array, window, delay):
for i in range(len(array)):
    for j in range(0, len(array)-i-1):
        if array[j] > array[j + 1]:
            temp = array[j]
            array[j] = array[j + 1]
            array[j + 1] = temp
            pygame.draw.rect(screen, RED, (15 + 15*j, 0, 10, temp))
            pygame.display.update()

        # This block of code is what I use to show the array on screen each frame
        window.fill(GREEN)
        array_bars(heights, screen, WHITE) # drawing the array each frame
        pygame.time.delay(delay)
        pygame.display.update()


 # Merge sort
 def merge_sort_visual(array, window, delay):
 if len(array) > 1:
    mid = len(array) // 2
    left_array = array[:mid]
    right_array = array[mid:]

    merge_sort_visual(left_array, screen, 8)
    merge_sort_visual(right_array, screen, 8)

    left_index = 0
    right_index = 0
    sort_index = 0

    while left_index < len(left_array) and right_index < len(right_array):
        if left_array[left_index] <= right_array[right_index]:
            array[sort_index] = left_array[left_index]
            left_index += 1

        else:
            array[sort_index] = right_array[right_index]
            right_index += 1

        sort_index += 1

    while left_index < len(left_array):
        array[sort_index] = left_array[left_index]
        left_index += 1
        sort_index += 1

    while right_index < len(right_array):
        array[sort_index] = right_array[right_index]
        right_index += 1
        sort_index += 1

如上所示,我不確定將我放入冒泡排序中的代碼塊放在合並排序中的位置,我嘗試了一段時間,但每次都得到錯誤的結果。 任何幫助將不勝感激,謝謝。

使用氣泡,您只需處理一個數組。 使用歸並排序,您正在處理遞歸和 arrays 的幾層。 更新主數組是一個挑戰,因為每一層遞歸只能看到完整數組的一部分。

解決方案是將部分數組位置傳遞給子進程,以便子進程知道要更新完整數組的哪一部分。

這是說明這個想法的更新代碼:

 # Merge sort
 def merge_sort_visual(start, array, window, delay):  # pass the starting postion of this array within the full array (first call is 0)
 if len(array) > 1:
        mid = len(array) // 2
        left_array = array[:mid]
        right_array = array[mid:]

        # This block of code is what I use to show the array on screen each frame
        window.fill(GREEN) 
        # for this method, you will need to create a temporary copy of the full array to display on the screen 
        array_bars(start, array, heights, screen, WHITE) # update part of the full array on screen (start location and array length)
        pygame.time.delay(delay)
        pygame.display.update()

        merge_sort_visual(start, left_array, screen, 8)  # pass location within full array
        merge_sort_visual(start+mid, right_array, screen, 8)  # pass location within full array

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM