Così a naso mi parrebbe un normale Bubble sort.
Amaro C++, il gusto pieno dell'undefined behavior.
Regole del Forum