Sort an array of strings according to string lengths

Published on July 29, 2021
Last updated December 23, 2021

Given problem statement

Caroline Forbes is an intelligent girl, every time she wins in any contest or programme, and also solves complex problems so I want to give her a challenge problem that is Sort an array of strings according to string lengths. If you are smarter than her try to solve the Problem faster than her?

Example input and output

Input: {“You”, “are”, “beautiful”, “looking”}
Output: You are looking beautiful

what we need to do

we need to write a progrm that will sort an array of strings according to their string lengths.

To acheive this we will be using a optimized version of bubble sort algorithm

In the driver code we will have the array of strings and we will call the sorting method by passing in the array of strings and the size of the array

In the sorting function we will be implementing bubble sort.

Sorting algorithm

The sorting algorithm will have two nested for loops in the second loop instead of going from start i.e. j = 0 like in bubble sort we will be starting the second loop from the i+1 position this way we won’t have to go over the already sorted part of the array.

Besides this the rest of the algorithm is a simple bubble sort we will just compare the lengths of two elements in the array and change their positions if the length of the bottom string is greater than the length of the top string.

Java program to sort an array of strings according to string lengths

class sortArrayOfStrings {
    static void sort(String[] s, int n) {
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                String temp = "";
                if (s[i].length() > s[j].length()) {
                    temp = s[i];
                    s[i] = s[j];
                    s[j] = temp;
                }
            }
        }
    }

    public static void main(String args[]) {
        String[] arr = { "you", "are", "beautiful", "looking" };
        int n = arr.length;

        // calling the Function to perform sorting
        sort(arr, n);

        System.out.println("Sorted array of strings:");
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
}

python program to sort an array of strings according to string lengths

class sortArrayOfStringsAccordingToStringLengths:
    def sort(self, s, n):
        for i in range(n):
            for j in range(i+1, n):
                temp = ""
                if len(s[i]) > len(s[j]):
                    temp = s[i]
                    s[i] = s[j]
                    s[j] = temp

    def main(self):
        s = ["you", "are", "beautiful", "looking"]
        n = len(s)
        self.sort(s, n)
        print("Sorted array of strings:")
        for i in range(n):
            print(s[i], end=" ")

sortArrayOfStringsAccordingToStringLengths().main()

Output

Output when the input is { "you", "are", "beautiful", "looking" } output



Tags :