Sort first k values in ascending order and remaining n | Function to sort the array ; Store the k elements in an array ; Store the remaining n - k elements in an array ; sorting the array from 0 to k - 1 places ; sorting the array from k to n places ; storing the values in the final array arr ; printing the array ; Driver code
< ? php function printOrder ( $ arr , $ n , $ k ) { $ len1 = $ k ; $ len2 = $ n - $ k ; $ arr1 = array_fill ( 0 , $ k , 0 ) ; $ arr2 = array_fill ( 0 , ( $ n - $ k ) , 0 ) ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ arr1 [ $ i ] = $ arr [ $ i ] ; for ( $ i = $ k ; $ i < $ n ; $ i ++ ) $ arr2 [ $ i - $ k ] = $ arr [ $ i ] ; sort ( $ arr1 ) ; sort ( $ arr2 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ i < $ k ) $ arr [ $ i ] = $ arr1 [ $ i ] ; else { $ arr [ $ i ] = $ arr2 [ $ len2 - 1 ] ; $ len2 -= 1 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) print ( $ arr [ $ i ] . " ▁ " ) ; } $ arr = array ( 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , -1 ) ; $ k = 4 ; $ n = count ( $ arr ) ; printOrder ( $ arr , $ n , $ k ) ; ? >