Block swap algorithm for array rotation | Wrapper over the recursive function leftRotateRec ( ) It left rotates arr by d . ; * Return If number of elements to be rotated is zero or equal to array size ; * If number of elements to be rotated is exactly half of array size ; If A is shorter ; If B is shorter ; function to pran array ; * This function swaps d elements starting at * index fi with d elements starting at index si ; Driver Code
def leftRotate ( arr , d , n ) : NEW_LINE INDENT leftRotateRec ( arr , 0 , d , n ) ; NEW_LINE DEDENT def leftRotateRec ( arr , i , d , n ) : NEW_LINE INDENT if ( d == 0 or d == n ) : NEW_LINE INDENT return ; NEW_LINE DEDENT if ( n - d == d ) : NEW_LINE INDENT swap ( arr , i , n - d + i , d ) ; NEW_LINE return ; NEW_LINE DEDENT if ( d < n - d ) : NEW_LINE INDENT swap ( arr , i , n - d + i , d ) ; NEW_LINE leftRotateRec ( arr , i , d , n - d ) ; NEW_LINE DEDENT else : NEW_LINE INDENT swap ( arr , i , d , n - d ) ; NEW_LINE leftRotateRec ( arr , n - d + i , 2 * d - n , d ) ; NEW_LINE DEDENT DEDENT def printArray ( arr , size ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT def swap ( arr , fi , si , d ) : NEW_LINE INDENT for i in range ( d ) : NEW_LINE INDENT temp = arr [ fi + i ] ; NEW_LINE arr [ fi + i ] = arr [ si + i ] ; NEW_LINE arr [ si + i ] = temp ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; NEW_LINE leftRotate ( arr , 2 , 7 ) ; NEW_LINE printArray ( arr , 7 ) ; NEW_LINE DEDENT