📜  排序算法可视化:堆排序

📅  最后修改于: 2021-04-21 23:10:04             🧑  作者: Mango

像堆排序这样的算法可以通过可视化轻松理解。在本文中,已实现了可视化堆排序算法的程序。

图形用户界面(GUI)使用pygame库在Python实现。

方法:

生成随机数组,并用条形填充pygame窗口。竖条是垂直的直线,代表数组元素。

  • 将所有条形设置为绿色(未排序)。
  • 堆化数组以执行排序。
  • Heapify之后,大条开始,然后是小条。
  • 使用pygame.time.delay()减慢算法速度,以便我们可以看到排序过程。
  • 实现计时器以查看算法如何执行。
  • 使用“ pygame.event.get()”方法执行操作,该方法存储用户执行的所有事件,例如启动,重置。
  • 蓝色用于突出显示在特定时间进行排序所涉及的栏。
  • 橙色突出显示已排序的条。

观察结果:

从“堆排序”可视化中可以清楚地看到,与“插入”排序或“选择”排序等其他排序算法相比,“堆排序”非常快,并且与“合并”排序的速度相似。

例子:

在运行以下程序之前,请确保安装pygame库。

下面是上述可视化工具的实现:

Python
# Python implementation of the
# Sorting visualiser: Heap Sort
  
# Imports
import pygame
import random
import time
pygame.font.init()
startTime = time.time()
  
# Total window
screen = pygame.display.set_mode(
    (900, 650)
)
  
# Title and Icon
pygame.display.set_caption(
    "SORTING VISUALISER"
)
  
# Uncomment below lines for setting
# up the icon for the visuliser
# img = pygame.image.load('sorticon.png')
# pygame.display.set_icon(img)
  
# Boolean variable to run
# the program in while loop
run = True
  
# Window size and some initials
width = 900
length = 600
array = [0]*151
arr_clr = [(0, 204, 102)]*151
clr_ind = 0
clr = [(0, 204, 102), (255, 0, 0),
       (0, 0, 153), (255, 102, 0)]
fnt = pygame.font.SysFont("comicsans", 30)
fnt1 = pygame.font.SysFont("comicsans", 20)
  
# Function to generate new Array
def generate_arr():
    for i in range(1, 151):
        arr_clr[i] = clr[0]
        array[i] = random.randrange(1, 100)
  
  
# Initially generate a array
generate_arr()
  
# Function to refill the
# updates on the window
def refill():
    screen.fill((255, 255, 255))
    draw()
    pygame.display.update()
    pygame.time.delay(10)
  
  
# Sorting Algorithm: Heap Sort
def heapSort(array):
    n = len(array)
    for i in range(n//2-1, -1, -1):
        pygame.event.pump()
        heapify(array, i, n)
    for i in range(n-1, 0, -1):
        array[i], array[0] = array[0], array[i]
        arr_clr[i] = clr[1]
        refill()
        heapify(array, 0, i)
  
  
def heapify(array, root, size):
    left = root * 2 + 1
    right = root * 2 + 2
    largest = root
    if left < size and array[left] > array[largest]:
        largest = left
    if right < size and array[right] > array[largest]:
        largest = right
    if largest != root:
        arr_clr[largest] = clr[2]
        arr_clr[root] = clr[2]
        array[largest],\
        array[root] = array[root],\
        array[largest]
        refill()
        arr_clr[largest] = clr[0]
        arr_clr[root] = clr[0]
        heapify(array, largest, size)
        refill()
  
# Function to Draw the array values
def draw():
  
    # Text should be rendered
    txt = fnt.render("SORT: PRESS 'ENTER'",
                     1, (0, 0, 0))
    # Position where text is placed
    screen.blit(txt, (20, 20))
    txt1 = fnt.render("NEW ARRAY: PRESS 'R'",
                      1, (0, 0, 0))
    screen.blit(txt1, (20, 40))
    txt2 = fnt1.render("ALGORITHM USED:" +
                       "HEAP SORT", 1, (0, 0, 0))
    screen.blit(txt2, (600, 60))
    text3 = fnt1.render("Running Time(sec): " +
                        str(int(time.time() - startTime)),
                        1, (0, 0, 0))
    screen.blit(text3, (600, 20))
    element_width = (width-150)//150
    boundry_arr = 900 / 150
    boundry_grp = 550 / 100
    pygame.draw.line(screen, (0, 0, 0), (0, 95),
                     (900, 95), 6)
  
    # Drawing the array values as lines
    for i in range(1, 151):
        pygame.draw.line(screen, arr_clr[i],
                         (boundry_arr * i-3, 100),
                         (boundry_arr * i-3,
                          array[i]*boundry_grp + 100),\
                         element_width)
  
  
# Program should be run
# continuously to keep the window open
while run:
    # background
    screen.fill((255, 255, 255))
  
    # Event handler stores all event
    for event in pygame.event.get():
  
        # If we click Close button in window
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                generate_arr()
            if event.key == pygame.K_RETURN:
                heapSort(array)
    draw()
    pygame.display.update()
  
pygame.quit()