Sort the given string using character search | Python 3 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 program to test above
def sortString ( st , n ) : NEW_LINE ' NEW_LINE INDENT arr = [ 0 ] * 26 NEW_LINE new_str = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ ord ( st [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT while ( arr [ i ] > 0 ) : NEW_LINE INDENT new_str += chr ( i + ord ( ' a ' ) ) NEW_LINE arr [ i ] -= 1 NEW_LINE DEDENT DEDENT return new_str NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " geeksforgeeks " NEW_LINE n = len ( st ) NEW_LINE print ( sortString ( st , n ) ) NEW_LINE DEDENT