Hello!
So I'm trying to implement bubble sort algorithm. I've created a vector in which I'm appending some rectangles and I'll sort them by their heights but it doesn't seem to sort anything. Am I doing something wrong? Please test this code... it's not too much.
#include <SFML/Graphics.hpp>
#include <ctime>
#include <iostream>
#include <vector>
int main()
{
srand(time(0));
float randomHeight;
std::vector<sf::RectangleShape> v;
float currY = 100;
sf::RenderWindow window(sf::VideoMode(1280, 800), "SFML works!");
for (unsigned int i = 0; i < 100; i++)
{
randomHeight = rand() % 500 + 200;
sf::RectangleShape rect({ 4, randomHeight });
rect.setFillColor(sf::Color::White);
rect.setRotation(180);
currY += 10;
rect.setPosition({ currY, 720 });
v.push_back(rect);
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
for (unsigned int i = 0; i < v.size(); i++)
{
for (auto itj = v.begin(); itj != v.end() - 1; itj = std::next(itj))
{
if ((*itj).getSize().y > (*std::next(itj)).getSize().y)
{
std::swap(*itj, *std::next(itj));
}
}
}
for (auto i = v.begin(); i != v.end(); i = std::next(i))
window.draw(*i);
window.display();
}
return 0;
}