Open In App

Python Program to Sort Words in Alphabetical Order

Last Updated : 07 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an input string, our task is to write a program to sort the words in alphabetic order using Python.

Examples:

Input : “geeks for Geeks”

Output : “for geeks geeks”  

Input : “the Quick brown fox jumPs over the lazY Dog”

Output : “brown dog fox jumps lazy over quick the the”

Sort Words in Alphabetic Order using sorted()

Python sorted() is a predefined function in Python that returns the sorted list of any particular sequence.

Python3




# Python3 program to sort the words of a string in
# alphabetical order
 
# Function to sort the words in alphabetical order
def Func(S):
  W = S.split(" ")
  for i in range(len(W)):
     
      # convert all the words into lowercase
      W[i]=W[i].lower() 
  S = sorted(W)
  print(' '.join(S))
 
# Driver code
S = "the Quick brown fox jumPs over the lazY Dog"
 
# function call
Func(S)


Output:

brown dog fox jumps lazy over quick the the

Time Complexity: O(n*logn)

Auxiliary Space: O(n)

Sort Words in Alphabetic Order using sort()

Python sort() function can be used to sort a list in ascending, descending, or user-defined order. Using the Python split() method the string is converted into a list of words. The split() method splits the string at whitespaces.

Python3




# Python3 program to sort the words of a
# string in alphabetical order
 
# Function to sort the words in alphabetical
# order
def F(S):
    W = S.split(" ")
    for i in range(len(W)):
        W[i] = W[i].lower()
    W.sort()
 
    # return the sorted words
    return ' '.join(W)
 
# Driver code
S = "GeekS for geEks"
print(F(S))


Output:

for geeks geeks

The time and space complexity of all the methods is the same:

Time Complexity: O(n*logn)

Auxiliary Space: O(n)

Using insertion sort:

Approach:

Split the input string into words using the split() function.
Get the length of the list of words.
Loop over the list of words, starting from the second word (i.e., index 1).
Assign the current word to a variable key.
Set a variable j to the index of the word immediately before the current word (i.e., index i-1).
While j is greater than or equal to 0 and the word at index j is greater than key, shift the word at index j one position to the right (i.e., assign the value of the word at index j to the word at index j+1) and decrement j.
Assign key to the position j+1.
Continue looping over the list of words until all words have been sorted.
Use the join() function to join the sorted list of words into a single string, separated by spaces.
Return the sorted string of words.

Python3




def sort_words_alphabetically(input_string):
    words = input_string.split()
    n = len(words)
    for i in range(1, n):
        key = words[i]
        j = i - 1
        while j >= 0 and words[j] > key:
            words[j+1] = words[j]
            j -= 1
        words[j+1] = key
    return ' '.join(words)
 
input_string = 'the Quick brown fox jumPs over the lazY Dog'
output_string = sort_words_alphabetically(input_string)
print(output_string)


Output

Dog Quick brown fox jumPs lazY over the the

Time Complexity: O(n^2)
Auxiliary Space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads