import pygame
import random
import time
# Настройки окна
WIDTH, HEIGHT = 800, 600
ARRAY_SIZE = 50
BAR_WIDTH = WIDTH // ARRAY_SIZE
# Цвета
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (100, 100, 255)
RED = (255, 100, 100)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Алгоритмы сортировки")
font = pygame.font.Font(None, 36)
# Генерация случайного массива
def generate_array():
return [random.randint(10, HEIGHT - 100) for _ in range(ARRAY_SIZE)]
array = generate_array()
# кнопки
button_random = pygame.Rect(0, 550, 300, 30)
button_algorithm = pygame.Rect(320, 550, 200, 30)
button_sort = pygame.Rect(550, 550, 250, 30)
selected_algorithm = "Пузырьком"
algorithms = ["Пузырько", "Гномья" ,"Быстрая", "Поразрядная"]
algorithm_index = 0
# Функция сортировки пузырьком с визуализацией
def bubble_sort_visual():
global array
n = len(array)
for i in range(n - 1):
for j in range(n - 1 - i):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
screen.fill(WHITE)
for k, value in enumerate(array):
color = RED if k == j or k == j + 1 else BLUE
pygame.draw.rect(screen, color, (k * BAR_WIDTH, HEIGHT - value - 50, BAR_WIDTH - 2, value))
pygame.draw.rect(screen, RED, button_random)
pygame.draw.rect(screen, RED, button_algorithm)
pygame.draw.rect(screen, RED, button_sort)
screen.blit(font.render("Генерировать массив", True, WHITE), (button_random.x + 30, button_random.y + 5))
screen.blit(font.render("Отсортировать", True, WHITE), (button_sort.x + 30, button_sort.y + 5))
screen.blit(font.render(selected_algorithm, True, WHITE), (button_algorithm.x + 30, button_algorithm.y + 5))
pygame.display.flip()
time.sleep(0.02)
running = True
while running:
screen.fill(WHITE)
# Отрисовка массива
for i, value in enumerate(array):
pygame.draw.rect(screen, BLUE, (i * BAR_WIDTH, HEIGHT - value - 50, BAR_WIDTH - 2, value))
# Отрисовка кнопок
pygame.draw.rect(screen, RED, button_random)
pygame.draw.rect(screen, RED, button_algorithm)
pygame.draw.rect(screen, RED, button_sort)
screen.blit(font.render("Генерировать массив", True, WHITE), (button_random.x + 30, button_random.y + 5))
screen.blit(font.render("Отсортировать", True, WHITE), (button_sort.x + 30, button_sort.y + 5))
screen.blit(font.render(selected_algorithm, True, WHITE), (button_algorithm.x + 30, button_algorithm.y + 5))
# Обработка событий
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if button_random.collidepoint(event.pos):
array = generate_array()
elif button_algorithm.collidepoint(event.pos):
algorithm_index = (algorithm_index + 1) % len(algorithms)
selected_algorithm = algorithms[algorithm_index]
elif button_sort.collidepoint(event.pos):
bubble_sort_visual()
pygame.display.flip()
pygame.quit()
#include
#include
#include
#include
#include
#include
#include
const int WIDTH = 800;
const int HEIGHT = 600;
const int ARRAY_SIZE = 50;
const int BAR_WIDTH = WIDTH / ARRAY_SIZE;
sf::Font font;
std::vector array;
std::vector algorithms = { L"Пузырьком", L"Гномья", L"Быстрая", L"Поразрядная" };
int algorithm_index = 0;
void generateArray() {
array.clear();
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution dist(10, HEIGHT - 100);
for (int i = 0; i < ARRAY_SIZE; i++) {
array.push_back(dist(gen));
}
}
void bubbleSort(sf::RenderWindow& window) {
int n = array.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (array[j] > array[j + 1]) {
std::swap(array[j], array[j + 1]);
window.clear(sf::Color::White);
for (int k = 0; k < n; k++) {
sf::RectangleShape bar(sf::Vector2f(BAR_WIDTH - 2, array[k]));
bar.setPosition(k * BAR_WIDTH, HEIGHT - array[k] - 50);
bar.setFillColor((k == j || k == j + 1) ? sf::Color::Red : sf::Color::Blue);
window.draw(bar);
}
window.display();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
}
}
}
int main() {
setlocale(LC_ALL, "ru_RU.UTF-8");
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Алгоритмы сортировки");
if (!font.loadFromFile("arial.ttf")) {
return -1;
}
generateArray();
sf::RectangleShape buttonRandom(sf::Vector2f(300, 30));
buttonRandom.setPosition(0, 550);
buttonRandom.setFillColor(sf::Color::Red);
sf::RectangleShape buttonAlgorithm(sf::Vector2f(200, 30));
buttonAlgorithm.setPosition(320, 550);
buttonAlgorithm.setFillColor(sf::Color::Red);
sf::RectangleShape buttonSort(sf::Vector2f(250, 30));
buttonSort.setPosition(550, 550);
buttonSort.setFillColor(sf::Color::Red);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::MouseButtonPressed) {
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
if (buttonRandom.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
generateArray();
}
else if (buttonAlgorithm.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
algorithm_index = (algorithm_index + 1) % algorithms.size();
}
else if (buttonSort.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
bubbleSort(window);
}
}
}
window.clear(sf::Color::White);
for (int i = 0; i < ARRAY_SIZE; i++) {
sf::RectangleShape bar(sf::Vector2f(BAR_WIDTH - 2, array[i]));
bar.setPosition(i * BAR_WIDTH, HEIGHT - array[i] - 50);
bar.setFillColor(sf::Color::Blue);
window.draw(bar);
}
window.draw(buttonRandom);
window.draw(buttonAlgorithm);
window.draw(buttonSort);
sf::Text text;
text.setFont(font);
text.setCharacterSize(20);
text.setFillColor(sf::Color::White);
text.setString(sf::String(L"Генерировать массив"));
text.setPosition(30, 555);
window.draw(text);
text.setString(sf::String(algorithms[algorithm_index]));
text.setPosition(350, 555);
window.draw(text);
text.setString(sf::String(L"Отсортировать"));
text.setPosition(580, 555);
window.draw(text);
window.display();
}
return 0;
}