We are given an array of strings, we need to sort the array in increasing order of string lengths.
Examples:
Input : {"GeeksforGeeeks", "I", "from", "am"}
Output : I am from GeeksforGeeks
Input : {"You", "are", "beautiful", "looking"}
Output : You are looking beautiful
A simple solution is to write our own sort function that compares string lengths to decide which string should come first. Below is the implementation that uses Insertion Sort to sort the array.
C++
#include<iostream>
using namespace std;
void printArraystring(string, int );
void sort(string s[], int n)
{
for ( int i=1 ;i<n; i++)
{
string temp = s[i];
int j = i - 1;
while (j >= 0 && temp.length() < s[j].length())
{
s[j+1] = s[j];
j--;
}
s[j+1] = temp;
}
}
void printArraystring(string str[], int n)
{
for ( int i=0; i<n; i++)
cout << str[i] << " " ;
}
int main()
{
string arr[] = { "GeeksforGeeks" , "I" , "from" , "am" };
int n = sizeof (arr)/ sizeof (arr[0]);
sort(arr, n);
printArraystring(arr, n);
return 0;
}
|
Java
import java.util.*;
class solution
{
static void sort(String []s, int n)
{
for ( int i= 1 ;i<n; i++)
{
String temp = s[i];
int j = i - 1 ;
while (j >= 0 && temp.length() < s[j].length())
{
s[j+ 1 ] = s[j];
j--;
}
s[j+ 1 ] = temp;
}
}
static void printArraystring(String str[], int n)
{
for ( int i= 0 ; i<n; i++)
System.out.print(str[i]+ " " );
}
public static void main(String args[])
{
String []arr = { "GeeksforGeeks" , "I" , "from" , "am" };
int n = arr.length;
sort(arr,n);
printArraystring(arr, n);
}
}
|
Python3
def printArraystring(string, n):
for i in range (n):
print (string[i], end = " " )
def sort(s, n):
for i in range ( 1 , n):
temp = s[i]
j = i - 1
while j > = 0 and len (temp) < len (s[j]):
s[j + 1 ] = s[j]
j - = 1
s[j + 1 ] = temp
if __name__ = = "__main__" :
arr = [ "GeeksforGeeks" , "I" , "from" , "am" ]
n = len (arr)
sort(arr, n)
printArraystring(arr, n)
|
C#
using System;
public class solution{
static void sort(String []s, int n)
{
for ( int i=1 ;i<n; i++)
{
String temp = s[i];
int j = i - 1;
while (j >= 0 && temp.Length < s[j].Length)
{
s[j+1] = s[j];
j--;
}
s[j+1] = temp;
}
}
static void printArraystring(String []str, int n)
{
for ( int i=0; i<n; i++)
Console.Write(str[i]+ " " );
}
public static void Main()
{
String []arr = { "GeeksforGeeks" , "I" , "from" , "am" };
int n = arr.Length;
sort(arr,n);
printArraystring(arr, n);
}
}
|
Javascript
<script>
function sort(s, n)
{
for (let i = 1 ; i < n; i++)
{
let temp = s[i];
let j = i - 1;
while (j >= 0 && temp.length < s[j].length)
{
s[j + 1] = s[j];
j--;
}
s[j + 1] = temp;
}
}
function printArraystring(str, n)
{
for (let i = 0; i < n; i++)
document.write(str[i]+ " " );
}
let arr = [ "GeeksforGeeks" , "I" , "from" , "am" ];
let n = arr.length;
sort(arr,n);
printArraystring(arr, n);
</script>
|
Output
I am from GeeksforGeeks
A better solution is to use sort function provided by programming languages like C++, Java. These functions also allow us to write our own custom comparator. Below is C++ implementation that uses C++ STL Sort function.
CPP
#include <bits/stdc++.h>
using namespace std;
bool compare(string &s1,string &s2)
{
return s1.size() < s2.size();
}
void printArraystring(string str[], int n)
{
for ( int i=0; i<n; i++)
cout << str[i] << " " ;
}
int main()
{
string arr[] = { "GeeksforGeeks" , "I" , "from" , "am" };
int n = sizeof (arr)/ sizeof (arr[0]);
sort(arr, arr+n, compare);
printArraystring(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Comparator;
class GFG {
static void printArrayString(String str[], int n) {
for ( int i = 0 ; i < n; i++)
System.out.print(str[i] + " " );
}
public static void main(String[] args) {
String arr[] = { "GeeksforGeeks" , "I" , "from" , "am" };
int n = arr.length;
Arrays.sort(arr, new Comparator<String>() {
@Override
public int compare( final String s1, final String s2) {
return s1.length() < s2.length() ? - 1 : 1 ;
}
});
printArrayString(arr, n);
}
}
|
Javascript
<script>
function compare(s1, s2) {
return s1.length - s2.length;
}
function printArraystring(str, n) {
for (let i = 0; i < n; i++)
document.write(str[i] + " " );
}
let arr = [ "GeeksforGeeks" , "I" , "from" , "am" ]
let n = arr.length
arr.sort(compare);
printArraystring(arr, n);
</script>
|
Python3
from functools import cmp_to_key
def compare(s1,s2):
return len (s1) - len (s2)
def printArraystring( str ,n):
for i in range (n):
print ( str [i],end = " " )
arr = [ "GeeksforGeeks" , "I" , "from" , "am" ]
n = len (arr)
arr.sort(key = cmp_to_key(compare))
printArraystring(arr, n)
|
Output
I am from GeeksforGeeks
Method #2: Simplified solution using sorted() function in python
- Take string as list.
- Use the sorted function in python by providing key as len.
Below is the implementation:
Python3
def printsorted(arr):
print ( * sorted (arr, key = len ))
arr = [ "GeeksforGeeks" , "I" , "from" , "am" ]
printsorted(arr)
|
Output
I am from GeeksforGeeks
This article is contributed by Rishabh jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.