Move all negative numbers to beginning and positive to end with constant extra space | A PHP program to put all negative numbers before positive numbers ; A utility function to print an array ; Driver code
< ? php function rearrange ( & $ arr , $ n ) { $ j = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] < 0 ) { if ( $ i != $ j ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j ] ; $ arr [ $ j ] = $ temp ; } $ j ++ ; } } } function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( -1 , 2 , -3 , 4 , 5 , 6 , -7 , 8 , 9 ) ; $ n = sizeof ( $ arr ) ; rearrange ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; ? >