Program to sort an array of strings using Selection Sort | C # 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
using System ; class GFG { 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 ; } } } static void Main ( ) { string [ ] arr = { " GeeksforGeeks " , " Practice . GeeksforGeeks " , " GeeksQuiz " } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . WriteLine ( i + " : ▁ " + arr [ i ] ) ; } Console . WriteLine ( ) ; selectionSort ( arr , n ) ; Console . WriteLine ( " Sorted ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . WriteLine ( i + " : ▁ " + arr [ i ] ) ; } } }