Print array of strings in sorted order without copying one string into another | function to print strings in sorted order ; Initially the index of the strings are assigned to the ' index [ ] ' ; selection sort technique is applied ; with the help of ' index [ ] ' strings are being compared ; index of the smallest string is placed at the ith index of ' index [ ] ' ; printing strings in sorted order ; Driver program to test above
function printInSortedOrder ( arr , n ) { let index = new Array ( n ) ; let i , j , min ; for ( i = 0 ; i < n ; i ++ ) { index [ i ] = i ; } for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ index [ min ] ] > ( arr [ index [ j ] ] ) ) { min = j ; } } if ( min != i ) { let temp = index [ min ] ; index [ min ] = index [ i ] ; index [ i ] = temp ; } } for ( i = 0 ; i < n ; i ++ ) { document . write ( arr [ index [ i ] ] + " " ) ; } } let arr = [ " " , " " , " " , " " ] ; let n = 4 ; printInSortedOrder ( arr , n ) ;