Rearrange positive and negative numbers using inbuilt sort function | Python3 implementation of the above approach ; Rearrange the array with all negative integers on left and positive integers on right use recursion to split the array with first element as one half and the rest array as another and then merge it with head of the array in each step ; exit condition ; rearrange the array except the first element in each recursive call ; If the first element of the array is positive , then right - rotate the array by one place first and then reverse the merged array . ; Driver code
def printArray ( array , length ) : NEW_LINE INDENT print ( " [ " , end = " " ) NEW_LINE for i in range ( length ) : NEW_LINE INDENT print ( array [ i ] , end = " " ) if ( i < ( length - 1 ) ) : print ( " , " , end = " ▁ " ) else : print ( " ] " ) NEW_LINE DEDENT DEDENT def reverse ( array , start , end ) : NEW_LINE INDENT while ( start < end ) : NEW_LINE INDENT temp = array [ start ] NEW_LINE array [ start ] = array [ end ] NEW_LINE array [ end ] = temp NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT DEDENT def rearrange ( array , start , end ) : NEW_LINE INDENT if ( start == end ) : NEW_LINE INDENT return NEW_LINE DEDENT rearrange ( array , ( start + 1 ) , end ) NEW_LINE if ( array [ start ] >= 0 ) : NEW_LINE INDENT reverse ( array , ( start + 1 ) , end ) NEW_LINE reverse ( array , start , end ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT array = [ - 12 , - 11 , - 13 , - 5 , - 6 , 7 , 5 , 3 , 6 ] NEW_LINE length = len ( array ) NEW_LINE countNegative = 0 NEW_LINE for i in range ( length ) : NEW_LINE INDENT if ( array [ i ] < 0 ) : NEW_LINE INDENT countNegative += 1 NEW_LINE DEDENT DEDENT print ( " array : ▁ " , end = " " ) NEW_LINE printArray ( array , length ) NEW_LINE rearrange ( array , 0 , ( length - 1 ) ) NEW_LINE reverse ( array , countNegative , ( length - 1 ) ) NEW_LINE print ( " rearranged ▁ array : ▁ " , end = " " ) NEW_LINE printArray ( array , length ) NEW_LINE DEDENT