Visualize MergeSort in Python

杀马特。学长 韩版系。学妹 提交于 2021-02-11 14:41:02

问题


I want to make a mergesort visualizer in python. I want to use turtle module. To draw a single bar i use the function draw_bar, and to draw the entire array there is the function draw_bars. Here is the code

def draw_bar(x,y,w,h):
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(0)
    turtle.down()
    turtle.begin_fill()
    turtle.fd(w)
    turtle.left(90)
    turtle.fd(h)
    turtle.left(90)
    turtle.fd(w)
    turtle.left(90)
    turtle.fd(h)
    turtle.left(90)
    turtle.end_fill()

def draw_bars(v,currenti=-1,currentj=-1,M=500):
    turtle.clear()
    x = -250
    n = len(v)
    w = 500/n
    r = 500/M
    for  i in range(n):
        if i == currenti: turtle.fillcolor('red')
        elif i == currentj: turtle.fillcolor('blue')
        else: turtle.fillcolor('gray')
        draw_bar(x,-250,w,v[i]*r)
        x += w
    screen.update()

Now i have this merge sort algorithm:

def mergeSort(arr):
    if len(arr) > 1:
        mid = len(arr)//2
        L = arr[:mid]
        R = arr[mid:]
        mergeSort(L)
        mergeSort(R)
        i=0
        j=0
        k=0
        while i < len(L) and j < len(R):
            if L[i] < R[j]:
                arr[k] = L[i]
                i += 1
            else:
                arr[k] = R[j]
                j += 1
            k += 1
        while i < len(L):
            arr[k] = L[i]
            i += 1
            k += 1
        while j < len(R):
            arr[k] = R[j]
            j += 1
            k += 1

Now I need to know ho to put the part of the code that refresh the visualised list.


回答1:


You would need to update your grafics every time your array is changed, meaning after each arr[k] = L[j]

This is however difficult to do in your current implementation, because your recursive function has no information about which part of the larger list it is operating on.

I would recommend to change the function so that it is always passed the compleat array as well as the start index and length of the part it will be working on:

def mergeSort(arr, start, length):
    if length > 1:
        mergeSort(arr, start, length/2)
        mergeSort(arr, start+length/2, length/2)
        etc.

Then you will be able to call drawbars every time, your array changes.

EDIT:

The full code would look something like this:

def mergeSort(arr, start, length):
    if length > 2:
        mergeSort(arr, start, int(length/2))
        mergeSort(arr, start+int(length/2), int(length/2))
    
    print(start+int(length/2))
    L = arr[start:start+int(length/2)]
    R = arr[start+int(length/2):start+length]
    i=0
    j=0
    k=0
    while i < len(L) and j < len(R):
        if L[i] < R[j]:
            arr[start+k] = L[i]
            draw_bars(myarray)
            i += 1
        else:
            arr[start+k] = R[j]
            draw_bars(myarray)
            j += 1
        k += 1
    while i < len(L):
        arr[start+k] = L[i]
        draw_bars(myarray)
        i += 1
        k += 1
    while j < len(R):
        arr[start+k] = R[j]
        draw_bars(myarray)
        j += 1
        k += 1
        
myarr = [2,345,2456,3456,56,34,5,78,34,5423,26487,324,1,3,4,5]
draw_bars(myarray)
mergeSort(myarr, 0 ,len(myarr))
print(myarr)


来源:https://stackoverflow.com/questions/65530510/visualize-mergesort-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!