Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
2.7 kB
1
Indexable
Never
#include <iostream>
#include <SFML/Graphics.hpp>
#include <vector>
#include <ctime>
#include "windows.h"

const int Width = 600, Height = 400;

sf::RenderWindow window(sf::VideoMode(Width, Height), "Sorting");

std::vector<int> randomVector(int lenght) {

    srand(time(0));

    std::vector<int> numbers;
    int random = 0;

    for (int i = 1; i < lenght + 1; i++) {
        numbers.push_back(i);
    }
    for (int i = 0; i < lenght; i++) {
        random = rand() % lenght;
        if (random != i) {
            std::swap(numbers[i], numbers[random]);
        }
    }

    return numbers;

}

void displaySorting(std::vector<int> list) {

    int len = end(list) - begin(list);
    float block = Width / len;

    std::vector<sf::RectangleShape> rect(len);

    window.clear();
    for (int i = 0; i < len; i++) {
        rect[i].setSize(sf::Vector2f(block, -Height / float((len / float(list[i])))));
        rect[i].setPosition((i  * block),400);
        rect[i].setFillColor(sf::Color::White);
        window.draw(rect[i]);
    }
    window.display();

    Sleep(5);
}

void CombSort(std::vector<int> values) {
    const double factor = 1.247; // Фактор уменьшения
    double step = values.size() - 1;

    while (step >= 1) {
        for (int i = 0; i + step < values.size(); ++i) {
            if (values[i] > values[i + step]) {
                std::swap(values[i], values[i + step]);
                displaySorting(values);
            }
        }
        step /= factor;
    }
    // сортировка пузырьком
    for (size_t idx_i = 0; idx_i + 1 < values.size(); ++idx_i) {
        for (size_t idx_j = 0; idx_j + 1 < values.size() - idx_i; ++idx_j) {
            if (values[idx_j + 1] < values[idx_j]) {
                std::swap(values[idx_j], values[idx_j + 1]);
                displaySorting(values);
            }
        }
    }
}
std::vector<int> bubbleSort(std::vector<int> list) {

    int length = end(list) - begin(list);
    for (int i = 0; i < length - 1; i++)
        for (int j = 0; j < length - i - 1; j++) {
            if (list[j] > list[j + 1]) {
                std::swap(list[j], list[j + 1]);
                displaySorting(list);

            }
        }
    return list;
}


int main() {

    const int nNumbers = 100;

    

    CombSort(randomVector(nNumbers));

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            
        }


        window.clear();

        

        window.display();
    }
}
Leave a Comment