#include <iostream>
#include <cstdlib>
template <typename T> void selection_sort(T* begin, T* end, unsigned pos = 0u)
{
if(begin != end)
{
for(int i = pos; i < (end - begin) + pos; i++)
{
if(begin[pos] > begin[i]) std::swap(begin[pos], begin[i]);
}
selection_sort(begin + 1, end, pos);
}
}
int main(int argc, char** argv)
{
int itens[] = {1, 3, 5, 2, 4};
selection_sort(&itens[0], &itens[5], 0);
for(int i = 0; i < 5; i++)
{
std::cout << itens[i] << " ";
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGxpYj4KCnRlbXBsYXRlIDx0eXBlbmFtZSBUPiB2b2lkIHNlbGVjdGlvbl9zb3J0KFQqIGJlZ2luLCBUKiBlbmQsIHVuc2lnbmVkIHBvcyA9IDB1KSAKewogICAgaWYoYmVnaW4gIT0gZW5kKQogICAgewogICAgICAgIGZvcihpbnQgaSA9IHBvczsgaSA8IChlbmQgLSBiZWdpbikgKyBwb3M7IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKGJlZ2luW3Bvc10gPiBiZWdpbltpXSkgc3RkOjpzd2FwKGJlZ2luW3Bvc10sIGJlZ2luW2ldKTsKICAgICAgICB9CiAgICAgICAgc2VsZWN0aW9uX3NvcnQoYmVnaW4gKyAxLCBlbmQsIHBvcyk7CiAgICB9Cn0KCmludCBtYWluKGludCBhcmdjLCBjaGFyKiogYXJndikKewogICAgaW50IGl0ZW5zW10gPSB7MSwgMywgNSwgMiwgNH07CiAgICAKICAgIHNlbGVjdGlvbl9zb3J0KCZpdGVuc1swXSwgJml0ZW5zWzVdLCAwKTsKICAgIAogICAgZm9yKGludCBpID0gMDsgaSA8IDU7IGkrKykKICAgIHsKICAgICAgICBzdGQ6OmNvdXQgPDwgaXRlbnNbaV0gPDwgIiAiOwogICAgfQogICAgCiAgICByZXR1cm4gMDsKfQo=