Block swap algorithm for array rotation | JavaScript code for above implementation ; A is shorter ; B is shorter ; Finally , block swap A and B
function leftRotate ( arr , d , n ) { if ( d == 0 d == n ) return ; let i = d ; let j = n - d ; while ( i != j ) { if ( i < j ) { arr = swap ( arr , d - i , d + j - i , i ) ; j -= i ; } else { arr = swap ( arr , d - i , d , j ) ; i -= j ; } } arr = swap ( arr , d - i , d , i ) ; }