This portion deals with the lexicographical property . Now , we put a character in string X when either this character has smaller value than the character present there right now or if this is the last position for it to exchange , else we fix the character already present here in this position .
if ( countx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === ctrx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] || X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) > j ) { countx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] -- ; ctrx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] -- ; ctry [ j ] -- ; X [ i ] = String . fromCharCode ( " " . charCodeAt ( 0 ) + j ) ; } else { countx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] -- ; } } document . write ( " " + X . join ( " " ) + " " ) ; document . write ( " " + change / 2 ) ; }