Program to perform sorting on a given list of strings on the basis of length of strings that is the smallest length string should be the first string in the list and the largest length string should be the last string in the sorted list
Answers
Answered by
1
Python Program to perform sorting on a given list of strings on the basis of length of strings
Explanation:
Python Program to Sort an array of strings according to string lengths
# Function to print the sorted array of string
def displayResult(string, num):
for i in range(num):
print(string[i], end = " ")
# Function for Sorting the array of string on the basis of lengths
def sorting(st, no):
for i in range(1, no):
t = st[i]
j = i - 1
while j >= 0 and len(t) < len(st[j]):
st[j + 1] = st[j]
j -= 1
st[j + 1] = t
# Driver code
if __name__ == "__main__":
ar = ["Brainly", "I", "use", "love" , "to"]
n = len(ar)
#sorting function
sorting(ar, n)
# Function call to display result
displayResult(ar, n)
Output:
I to use love Brainly
Similar questions