Store suffixes and their indexes in an array of structures . The structure is needed to sort the suffixes alphabatically and maintain their old indexes while sorting
for ( int i = 0 ; i < n ; i ++ ) { suffixes [ i ] . index = i ; suffixes [ i ] . rank [ 0 ] = txt [ i ] - ' a ' ; suffixes [ i ] . rank [ 1 ] = ( ( i + 1 ) < n ) ? ( txt [ i + 1 ] - ' a ' ) : -1 ; }