Print array of strings in sorted order without copying one string into another | Java implementation to 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
class GFG { static void printInSortedOrder ( String arr [ ] , int n ) { int index [ ] = new int [ n ] ; int 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 ] ] . compareTo ( arr [ index [ j ] ] ) > 0 ) { min = j ; } } if ( min != i ) { int temp = index [ min ] ; index [ min ] = index [ i ] ; index [ i ] = temp ; } } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ index [ i ] ] + " ▁ " ) ; } } static public void main ( String [ ] args ) { String arr [ ] = { " geeks " , " quiz " , " geeks " , " for " } ; int n = 4 ; printInSortedOrder ( arr , n ) ; } }