Program to sort an array of strings using Selection Sort | Java program to implement selection sort on array of strings ; Sorts an array of strings ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; If arr [ j ] is smaller than minStr ; Make arr [ j ] as minStr and update min_idx ; Swapping the minimum element found with the first element . ; Driver code ; Printing the array before sorting ; Printing the array after sorting
import java . util . * ; import java . io . * ; class Main { static void selectionSort ( String arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { int min_index = i ; String minStr = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] . compareTo ( minStr ) < 0 ) { minStr = arr [ j ] ; min_index = j ; } } if ( min_index != i ) { String temp = arr [ min_index ] ; arr [ min_index ] = arr [ i ] ; arr [ i ] = temp ; } } } public static void main ( String args [ ] ) { String arr [ ] = { " GeeksforGeeks " , " Practice . GeeksforGeeks " , " GeeksQuiz " } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . println ( i + " : ▁ " + arr [ i ] ) ; } System . out . println ( ) ; selectionSort ( arr , n ) ; System . out . println ( " Sorted ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . println ( i + " : ▁ " + arr [ i ] ) ; } } }