'Sorting vector of blocks C++ SFML

I just wanted to make a little program to sort blocks using selection sort, but it doesn't sort itself. Blocks just keep switching places in some pattern, until I terminate the process.

I think there is something wrong with the swaping of the blocks, but to be honest I'm really lost.

I will appreciate any help from you guys.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace sf;
int main()
{
    RenderWindow win(VideoMode(200, 200), "SFML works!",Style::Titlebar);
    win.setFramerateLimit(60);

    std::vector<RectangleShape> blocks;
    for(int i=1;i<=10;i++)
    {
        RectangleShape block(sf::Vector2f(20,20*i));
        block.setOrigin(block.getSize().x,block.getSize().y);
        block.setOutlineColor(Color::Red);
        block.setOutlineThickness(0.5f);
        //block.setPosition(i*20,200);
        blocks.push_back(block);
    }
    std::random_shuffle(blocks.begin(),blocks.end());
    for(int i=1;i<=10;i++)
        blocks[i].setPosition(i*20,200);
    bool t = false;
    while (win.isOpen())
    {
        Event event;
        while (win.pollEvent(event))
        {
            if (event.type == Event::Closed)
                win.close();
            if(event.type == Event::KeyPressed)
            if (Keyboard::isKeyPressed(Keyboard::Space)){
                if(t){
                    t = false;
                    std::cout<<"KLIK"<<"\n";
                    break;
                }
                    t = true;

                std::cout<<"KLIK"<<"\n";
            }
        }

        win.clear();

        for(auto a:blocks)
            win.draw(a);
        if(t){
        int k;
        for(int i=0;i<blocks.size();i++)
        {
            win.clear();
            for(auto a:blocks)
                win.draw(a);
            win.display();
            k = i;
            for(int j=i+1;j<blocks.size();j++)
                if(blocks[j].getSize().y < blocks[k].getSize().y)
                    k = j;
            Vector2f temp = blocks[k].getPosition();
            blocks[k].setPosition(blocks[i].getPosition().x,blocks[i].getPosition().y);
            blocks[i].setPosition(temp.x,temp.y);
            //std::cout<<i<<"\n";
        }

    }

        win.display();
    }

    return 0;
}


Solution 1:[1]

First you have an exception out_of_range here:

for(int i=1;i<=10;i++)
    blocks[i].setPosition(i*20,200);

Remember that in c++ you index from 0 and when you use push_back() 10 times in the vector you have indexes from 0 to 9

Solution:

for(int i=0;i<10;i++)
    blocks[i].setPosition((i+1)*20,200);

Secondly, in selection sort, you still need to replace the indexes in the vector so that the sorted values are in front.

Solution:

Vector2f temp = blocks[k].getPosition();
blocks[k].setPosition(blocks[i].getPosition().x,blocks[i].getPosition().y);
blocks[i].setPosition(temp.x,temp.y);
std::swap(blocks[k], blocks[i]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Selvan