Program to sort an array of strings using Selection Sort | 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
function selectionSort ( arr , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) { let min_index = i ; let minStr = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { if ( ( arr [ j ] ) . localeCompare ( minStr ) === - 1 ) { minStr = arr [ j ] ; min_index = j ; } } if ( min_index != i ) { let temp = arr [ min_index ] ; arr [ min_index ] = arr [ i ] ; arr [ i ] = temp ; } } } let arr = [ " " , " " , " " ] ; let n = arr . length ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( i + " " + arr [ i ] + " " ) ; } document . write ( " " ) ; selectionSort ( arr , n ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( i + " " + arr [ i ] + " " ) ; }