Sort a binary array using one traversal | PHP Code for Sort a binary array using one traversal ; if number is smaller than 1 then swap it with j - th number ; Driver Code
< ? php function sortBinaryArray ( $ a , $ n ) { $ j = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] < 1 ) { $ j ++ ; $ temp = $ a [ $ j ] ; $ a [ $ j ] = $ a [ $ i ] ; $ a [ $ i ] = $ temp ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ a [ $ i ] . " ▁ " ; } $ a = array ( 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 ) ; $ n = count ( $ a ) ; sortBinaryArray ( $ a , $ n ) ; ? >