Rearrange array to make decimal equivalents of reversed binary representations of array elements sorted | Function to reverse the bits of a number ; Stores the reversed number ; Divide rev by 2 ; If the value of N is odd ; Update the value of N ; Return the final value of rev ; Function for rearranging the array element according to the given rules ; Stores the new array elements ; Function for rearranging the array ; Stores the new array ; Function to sort the array by reversing the binary representation ; Creating a new array ; Sort the array with the key ; Get arr from newArr ; Print the sorted array ; Driver Code
def keyFunc ( n ) : NEW_LINE INDENT rev = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT rev = rev << 1 NEW_LINE if ( n & 1 == 1 ) : NEW_LINE INDENT rev = rev ^ 1 NEW_LINE DEDENT n = n >> 1 NEW_LINE DEDENT return rev NEW_LINE DEDENT def getNew ( arr ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in arr : NEW_LINE INDENT ans . append ( [ keyFunc ( i ) , i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def getArr ( arr ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in arr : NEW_LINE INDENT ans . append ( i [ 1 ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def sortArray ( arr ) : NEW_LINE INDENT newArr = getNew ( arr ) NEW_LINE newArr . sort ( ) NEW_LINE arr = getArr ( newArr ) NEW_LINE print ( arr ) NEW_LINE DEDENT arr = [ 43 , 52 , 61 , 41 ] NEW_LINE sortArray ( arr ) NEW_LINE