Sort an array of strings according to string lengths | Function to print the sorted array of string ; Function to Sort the array of string according to lengths . This function implements Insertion Sort . ; Insert s [ j ] at its correct position ; Driver code ; Function to perform sorting ; Calling the function to print result
def printArraystring ( string , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( string [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def sort ( s , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT temp = s [ i ] NEW_LINE j = i - 1 NEW_LINE while j >= 0 and len ( temp ) < len ( s [ j ] ) : NEW_LINE INDENT s [ j + 1 ] = s [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT s [ j + 1 ] = temp NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " GeeksforGeeks " , " I " , " from " , " am " ] NEW_LINE n = len ( arr ) NEW_LINE sort ( arr , n ) NEW_LINE printArraystring ( arr , n ) NEW_LINE DEDENT