Since size of index is constant , below operations take constant time .
index . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < MAX_CHAR && index [ i ] != n ; i ++ ) document . write ( str [ index [ i ] ] ) ; }