Shuffle 2 n integers as a1 | Function to reverse the array from the position ' start ' to position 'end ; Stores mid of start and end ; Traverse the array in the range [ start , end ] ; Stores arr [ start + i ] ; Update arr [ start + i ] ; Update arr [ end - i ] ; Utility function to shuffle the given array in the of form { a1 , b1 , a2 , b2 , ... . an , bn } ; Stores the length of the array ; If length of the array is 2 ; Stores mid of the { start , end } ; Divide array into two halves of even length ; Update mid ; Calculate the mid - points of both halves of the array ; Reverse the subarray made from mid1 to mid2 ; Reverse the subarray made from mid1 to mid ; Reverse the subarray made from mid to mid2 ; Recursively calls for both the halves of the array ; Function to shuffle the given array in the form of { a1 , b1 , a2 , b2 , ... . an , bn } ; Function Call ; Print the modified array ; Driver Code ; Given array ; Size of the array ; Shuffles the given array to the required permutation
' NEW_LINE def reverse ( arr , start , end ) : NEW_LINE INDENT mid = ( end - start + 1 ) // 2 NEW_LINE for i in range ( mid ) : NEW_LINE INDENT temp = arr [ start + i ] NEW_LINE arr [ start + i ] = arr [ end - i ] NEW_LINE arr [ end - i ] = temp NEW_LINE DEDENT return arr NEW_LINE DEDENT def shuffleArrayUtil ( arr , start , end ) : NEW_LINE INDENT i = 0 NEW_LINE l = end - start + 1 NEW_LINE if ( l == 2 ) : NEW_LINE INDENT return NEW_LINE DEDENT mid = start + l // 2 NEW_LINE if ( l % 4 ) : NEW_LINE INDENT mid -= 1 NEW_LINE DEDENT mid1 = start + ( mid - start ) // 2 NEW_LINE mid2 = mid + ( end + 1 - mid ) // 2 NEW_LINE arr = reverse ( arr , mid1 , mid2 - 1 ) NEW_LINE arr = reverse ( arr , mid1 , mid - 1 ) NEW_LINE arr = reverse ( arr , mid , mid2 - 1 ) NEW_LINE shuffleArrayUtil ( arr , start , mid - 1 ) NEW_LINE shuffleArrayUtil ( arr , mid , end ) NEW_LINE DEDENT def shuffleArray ( arr , N , start , end ) : NEW_LINE INDENT shuffleArrayUtil ( arr , start , end ) NEW_LINE for i in arr : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 2 , 4 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE shuffleArray ( arr , N , 0 , N - 1 ) NEW_LINE DEDENT