private static < T > void permuteArray ( T [ ] arr , int cp ) { // Allow negative cp points for convenience. if ( cp == 0 ) { return ; } if ( cp < 0 ) { cp = arr . length + cp ; } if ( cp < 0 || cp >= arr . length ) { throw new ArrayIndexOutOfBoundsException ( "Permutation point (" + cp + ") must be between -ca2.length and ca2.length-1" ) ; } List < T > temp = new ArrayList < T > ( cp ) ; // shift residues left for ( int i = 0 ; i < cp ; i ++ ) { temp . add ( arr [ i ] ) ; } for ( int j = cp ; j < arr . length ; j ++ ) { arr [ j - cp ] = arr [ j ] ; } for ( int i = 0 ; i < cp ; i ++ ) { arr [ arr . length - cp + i ] = temp . get ( i ) ; } }
Circularly permutes arr in place .