Sort the given string using character search | Java implementation to sort the given String without using any sorting technique ; A character array to store the no . of occurrences of each character between ' a ' to ' z ' ; to store the final sorted String ; To store each occurrence of character by relative indexing ; To traverse the character array and append it to new_str ; Driver code
class GFG { static String sortString ( String str , int n ) { int i ; char [ ] arr = new char [ 26 ] ; String new_str = " " ; for ( i = 0 ; i < n ; i ++ ) ++ arr [ str . charAt ( i ) - ' a ' ] ; for ( i = 0 ; i < 26 ; i ++ ) while ( arr [ i ] -- > 0 ) { new_str += String . valueOf ( ( char ) ( i + ' a ' ) ) ; } return new_str ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; System . out . print ( sortString ( str , n ) ) ; } }