Print n smallest elements from given array in their original order | Function to print smallest n numbers ; Make copy of array ; Sort copy array ; For each arr [ i ] find whether it is a part of n - smallest with binary search ; Driver program
function printSmall ( arr , asize , n ) { let copy_arr = [ ... arr ] ; copy_arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < asize ; ++ i ) { if ( arr [ i ] < copy_arr [ n ] ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] ; let asize = arr . length ; let n = 5 ; printSmall ( arr , asize , n ) ;