Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要帮助实现与pygame合并排序的可视化工具吗_Python_Sorting_Pygame - Fatal编程技术网

Python 需要帮助实现与pygame合并排序的可视化工具吗

Python 需要帮助实现与pygame合并排序的可视化工具吗,python,sorting,pygame,Python,Sorting,Pygame,我目前正在使用pygame构建一个排序可视化工具,到目前为止,我已经能够进行气泡排序和选择排序。我现在正在做的是每次修改时都画一个数组的框架,这适用于气泡和选择排序。但是对于合并排序,总是有数组被拆分并合并回来,所以我不能每一帧都绘制数组,因为每一帧只显示数组的一部分。我目前一直在研究如何在合并排序上实现可视化工具,下面我将举一个例子,说明什么适用于冒泡排序,以及我目前在合并排序上使用的是什么 # Bubble sort def bubble_sort_visual(array, window,

我目前正在使用pygame构建一个排序可视化工具,到目前为止,我已经能够进行气泡排序和选择排序。我现在正在做的是每次修改时都画一个数组的框架,这适用于气泡和选择排序。但是对于合并排序,总是有数组被拆分并合并回来,所以我不能每一帧都绘制数组,因为每一帧只显示数组的一部分。我目前一直在研究如何在合并排序上实现可视化工具,下面我将举一个例子,说明什么适用于冒泡排序,以及我目前在合并排序上使用的是什么

# 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
#气泡排序
def气泡_排序_可视(阵列、窗口、延迟):
对于范围内的i(len(数组)):
对于范围(0,透镜(阵列)-i-1中的j:
如果数组[j]>数组[j+1]:
温度=阵列[j]
数组[j]=数组[j+1]
阵列[j+1]=温度
pygame.draw.rect(屏幕,红色,(15+15*j,0,10,温度))
pygame.display.update()
#这段代码是我用来在每一帧屏幕上显示数组的
窗口填充(绿色)
阵列_条(高度、屏幕、白色)#每帧绘制阵列
pygame.time.delay(延迟)
pygame.display.update()
#合并排序
def合并\排序\可视(阵列、窗口、延迟):
如果len(阵列)>1:
mid=len(数组)//2
左_数组=数组[:中间]
右数组=数组[mid:]
合并\排序\可视(左\数组,屏幕,8)
合并\排序\可视化(右\数组,屏幕,8)
左索引=0
右索引=0
排序索引=0
当左索引如果左数组[left_index]带有气泡,则只处理单个数组。使用合并排序,您将处理递归和多个数组层。更新主数组是一个挑战,因为递归的每一层只看到完整数组的一部分

解决方案是将部分数组位置传递给子进程,以便子进程知道要更新整个数组的哪个部分

下面是最新的代码来说明这个想法:

 # 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

可以有一个包含所有拆分数组的数组。然后有一个作用域向下的方法来显示它,而不是在每一个排序中显示它,只需从中调用thst方法。