📅  最后修改于: 2023-12-03 15:37:37.686000             🧑  作者: Mango
Pygame 是一个 Python 的游戏开发库,它为 Python 提供了编程游戏的功能。三元搜索是一种高效的搜索算法,它可以在有顺序的列表中进行搜索,通常用于大数据集和词汇表等。在这篇文章中,我们将使用 Pygame 来对三元搜索进行可视化。
在使用 Pygame 之前,需要先将其安装到 Python 系统中。可以在终端命令行中输入以下命令来安装 Pygame:
pip install pygame
以下是一个简单的三元搜索算法的示例代码:
def ternary_search(arr, val):
left = 0
right = len(arr) - 1
while left <= right:
mid1 = left + (right - left) // 3
mid2 = right - (right - left) // 3
if arr[mid1] == val:
return mid1
elif arr[mid2] == val:
return mid2
elif val < arr[mid1]:
right = mid1 - 1
elif val > arr[mid2]:
left = mid2 + 1
else:
left = mid1 + 1
right = mid2 - 1
return -1
接下来是我们编写的 Pygame 三元搜索可视化程序:
import pygame
import random
# 定义一些常量
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
ARRAY_LENGTH = 100
MIN_VALUE = 0
MAX_VALUE = 100
SEARCH_VALUE = random.randint(MIN_VALUE, MAX_VALUE)
FONT_NAME = pygame.font.match_font('arial')
FPS = 30
# 初始化 Pygame
pygame.init()
pygame.display.set_caption('Ternary Search Visualizer')
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
# 初始化字体
def draw_text(text, size, x, y):
font = pygame.font.Font(FONT_NAME,size)
text_surface = font.render(text,True,(255,255,255))
text_rect = text_surface.get_rect()
text_rect.midtop = (x,y)
screen.blit(text_surface,text_rect)
# 生成随机数组
def generate_array():
arr = []
for i in range(ARRAY_LENGTH):
arr.append(random.randint(MIN_VALUE, MAX_VALUE))
arr.sort()
return arr
# 绘制数组
def draw_array(arr):
for i in range(ARRAY_LENGTH):
height = arr[i]
pygame.draw.rect(screen, (0, 255, 0), (i * 6, SCREEN_HEIGHT - height, 5, height))
# 绘制搜索值
def draw_search_value(value):
draw_text('Searching for ' + str(value), 20, SCREEN_WIDTH // 2, 10)
# 绘制找不到值的提示
def draw_not_found():
draw_text('Value not found', 20, SCREEN_WIDTH // 2, 50)
# 绘制搜索范围
def draw_search_range(left, right):
pygame.draw.rect(screen, (255, 0, 0), (left * 6, 0, 5, SCREEN_HEIGHT))
pygame.draw.rect(screen, (255, 0, 0), (right * 6, 0, 5, SCREEN_HEIGHT))
# 绘制中间值
def draw_mid_value(mid1, mid2):
pygame.draw.rect(screen, (0, 0, 255), (mid1 * 6, 0, 5, SCREEN_HEIGHT))
pygame.draw.rect(screen, (0, 0, 255), (mid2 * 6, 0, 5, SCREEN_HEIGHT))
# 主循环
def main():
arr = generate_array()
left = 0
right = ARRAY_LENGTH - 1
mid1 = 0
mid2 = 0
found = False
while not found and left <= right:
screen.fill((0, 0, 0))
draw_array(arr)
draw_search_value(SEARCH_VALUE)
draw_search_range(left, right)
draw_mid_value(mid1, mid2)
mid1 = left + (right - left) // 3
mid2 = right - (right - left) // 3
if arr[mid1] == SEARCH_VALUE:
found = True
elif arr[mid2] == SEARCH_VALUE:
found = True
elif SEARCH_VALUE < arr[mid1]:
right = mid1 - 1
elif SEARCH_VALUE > arr[mid2]:
left = mid2 + 1
else:
left = mid1 + 1
right = mid2 - 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
return
pygame.display.flip()
clock.tick(FPS)
if found:
draw_text('Value found at index ' + str(mid1), 20, SCREEN_WIDTH // 2, 50)
else:
draw_not_found()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
pygame.display.flip()
clock.tick(FPS)
if __name__ == '__main__':
main()
运行程序后,会生成一个随机数组并显示在屏幕上,程序还会随机生成一个搜索的值。然后使用三元搜索算法进行搜索,在可视化搜索过程中高亮表示当前搜索的范围和中间值,最后显示搜索结果。如果搜索值不在数组中,程序将显示“Value not found”。
本文介绍了如何在 Python 中使用 Pygame 对三元搜索进行可视化。我们实现了一个可以生成随机数组并使用三元搜索算法进行搜索的程序。程序实现了一个动态的可视化搜索过程,能够更直观地展示搜索过程。