#include <iostream>
#include <algorithm>
#include <functional>
void print( const int a[], std::size_t n, std::size_t position_of_active_item )
{
std::cout << "{ " ;
for( std::size_t i = 0 ; i < n ; ++i )
if( i == position_of_active_item ) std::cout << '[' << a[i] << "] " ;
else std::cout << a[i] << ' ' ;
std::cout << "}\n" ;
}
int main()
{
int a[] = { -5, 16, 8, 9, -12, 0, -3 } ;
constexpr std::size_t N = sizeof(a) / sizeof(*a) ;
std::size_t position_of_active_item = 2 ;
print( a, N, position_of_active_item ) ;
// swap active item with the first element
std::swap( a[0], a[position_of_active_item] ) ;
position_of_active_item = 0 ;
// sort from the second item onwards
std::sort( a+1, a+N ) ;
print( a, N, position_of_active_item ) ;
// bring the active item into place
std::size_t i = 1 ;
for( ; a[i] < a[i-1] && i < N ; ++i ) std::swap( a[i], a[i-1] ) ;
position_of_active_item = i-1 ;
print( a, N, position_of_active_item ) ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KCnZvaWQgcHJpbnQoIGNvbnN0IGludCBhW10sIHN0ZDo6c2l6ZV90IG4sIHN0ZDo6c2l6ZV90IHBvc2l0aW9uX29mX2FjdGl2ZV9pdGVtICkKewogICBzdGQ6OmNvdXQgPDwgInsgIiA7CiAgIGZvciggc3RkOjpzaXplX3QgaSA9IDAgOyBpIDwgbiA7ICsraSApCiAgICAgICBpZiggaSA9PSBwb3NpdGlvbl9vZl9hY3RpdmVfaXRlbSApIHN0ZDo6Y291dCA8PCAnWycgPDwgYVtpXSA8PCAiXSAiIDsKICAgICAgIGVsc2Ugc3RkOjpjb3V0IDw8IGFbaV0gPDwgJyAnIDsKICAgc3RkOjpjb3V0IDw8ICJ9XG4iIDsKfQoKCmludCBtYWluKCkKewogICAgaW50IGFbXSA9IHsgLTUsIDE2LCA4LCA5LCAtMTIsIDAsIC0zIH0gOwogICAgY29uc3RleHByIHN0ZDo6c2l6ZV90IE4gPSBzaX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