public String toCycleString ( ) { int n = this . values . length ; boolean [ ] p = new boolean [ n ] ; Arrays . fill ( p , true ) ; StringBuilder sb = new StringBuilder ( ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] ) { sb . append ( ' ' ) ; sb . append ( i ) ; p [ i ] = false ; j = i ; while ( p [ values [ j ] ] ) { sb . append ( ", " ) ; j = values [ j ] ; sb . append ( j ) ; p [ j ] = false ; } sb . append ( ' ' ) ; } } return sb . toString ( ) ; }
An easily - readable version of the permutation as a product of cycles .