我们得到一个字符串数组,我们需要按照字符串长度的递增顺序对数组进行排序。 例如:
null
Input : {"GeeksforGeeeks", "I", "from", "am"}Output : I am from GeeksforGeeksInput : {"You", "are", "beautiful", "looking"}Output : You are looking beautiful
A. 简单解决方案 就是编写我们自己的排序函数,比较字符串长度,以决定哪个字符串应该先出现。下面是使用 插入排序 对数组进行排序。
C++
// C++ program to sort an Array of // Strings according to their lengths #include<iostream> using namespace std; // Function to print the sorted array of string void printArraystring(string, int ); // Function to Sort the array of string // according to lengths. This function // implements Insertion Sort. void sort(string s[], int n) { for ( int i=1 ;i<n; i++) { string temp = s[i]; // Insert s[j] at its correct position int j = i - 1; while (j >= 0 && temp.length() < s[j].length()) { s[j+1] = s[j]; j--; } s[j+1] = temp; } } // Function to print the sorted array of string void printArraystring(string str[], int n) { for ( int i=0; i<n; i++) cout << str[i] << " " ; } // Driver function int main() { string arr[] = { "GeeksforGeeks" , "I" , "from" , "am" }; int n = sizeof (arr)/ sizeof (arr[0]); // Function to perform sorting sort(arr, n); // Calling the function to print result printArraystring(arr, n); return 0; } |
JAVA
// Java program to sort an Array of // Strings according to their lengths import java.util.*; class solution { // Function to print the sorted array of string // void printArraystring(string,int); // Function to Sort the array of string // according to lengths. This function // implements Insertion Sort. static void sort(String []s, int n) { for ( int i= 1 ;i<n; i++) { String temp = s[i]; // Insert s[j] at its correct position int j = i - 1 ; while (j >= 0 && temp.length() < s[j].length()) { s[j+ 1 ] = s[j]; j--; } s[j+ 1 ] = temp; } } // Function to print the sorted array of string static void printArraystring(String str[], int n) { for ( int i= 0 ; i<n; i++) System.out.print(str[i]+ " " ); } // Driver function public static void main(String args[]) { String []arr = { "GeeksforGeeks" , "I" , "from" , "am" }; int n = arr.length; // Function to perform sorting sort(arr,n); // Calling the function to print result printArraystring(arr, n); } } |
Python3
# Python3 program to sort an Array of # Strings according to their lengths # Function to print the sorted array of string def printArraystring(string, n): for i in range (n): print (string[i], end = " " ) # Function to Sort the array of string # according to lengths. This function # implements Insertion Sort. def sort(s, n): for i in range ( 1 , n): temp = s[i] # Insert s[j] at its correct position j = i - 1 while j > = 0 and len (temp) < len (s[j]): s[j + 1 ] = s[j] j - = 1 s[j + 1 ] = temp # Driver code if __name__ = = "__main__" : arr = [ "GeeksforGeeks" , "I" , "from" , "am" ] n = len (arr) # Function to perform sorting sort(arr, n) # Calling the function to print result printArraystring(arr, n) # This code is contributed by # sanjeev2552 |
C#
// C# program to sort an Array of // Strings according to their lengths using System; public class solution{ // Function to print the sorted array of string // void printArraystring(string,int); // Function to Sort the array of string // according to lengths. This function // implements Insertion Sort. static void sort(String []s, int n) { for ( int i=1 ;i<n; i++) { String temp = s[i]; // Insert s[j] at its correct position int j = i - 1; while (j >= 0 && temp.Length < s[j].Length) { s[j+1] = s[j]; j--; } s[j+1] = temp; } } // Function to print the sorted array of string static void printArraystring(String []str, int n) { for ( int i=0; i<n; i++) Console.Write(str[i]+ " " ); } // Driver function public static void Main() { String []arr = { "GeeksforGeeks" , "I" , "from" , "am" }; int n = arr.Length; // Function to perform sorting sort(arr,n); // Calling the function to print result printArraystring(arr, n); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // Javascript program to sort an Array of // Strings according to their lengths // Function to print the sorted array of string // void printArraystring(string,int); // Function to Sort the array of string // according to lengths. This function // implements Insertion Sort. function sort(s, n) { for (let i = 1 ; i < n; i++) { let temp = s[i]; // Insert s[j] at its correct position let j = i - 1; while (j >= 0 && temp.length < s[j].length) { s[j + 1] = s[j]; j--; } s[j + 1] = temp; } } // Function to print the sorted array of string 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; // Function to perform sorting sort(arr,n); // Calling the function to print result printArraystring(arr, n); // This code is contributed by vaibhavrabadiya117. </script> |
输出
I am from GeeksforGeeks
A. 更好的解决方案 使用C++语言、java等编程语言提供的排序函数。这些函数还允许我们编写自己的自定义比较器。以下是使用的C++实现 C++ STL排序函数 .
CPP
#include <bits/stdc++.h> using namespace std; // Function to check the small string bool compare(string &s1,string &s2) { return s1.size() < s2.size(); } // Function to print the sorted array of string void printArraystring(string str[], int n) { for ( int i=0; i<n; i++) cout << str[i] << " " ; } // Driver function int main() { string arr[] = { "GeeksforGeeks" , "I" , "from" , "am" }; int n = sizeof (arr)/ sizeof (arr[0]); // Function to perform sorting sort(arr, arr+n, compare); // Calling the function to print result printArraystring(arr, n); return 0; } |
JAVA
import java.util.Arrays; import java.util.Comparator; class GFG { // Function to check the small String // Function to print the sorted array of String static void printArrayString(String str[], int n) { for ( int i = 0 ; i < n; i++) System.out.print(str[i] + " " ); } // Driver function public static void main(String[] args) { String arr[] = { "GeeksforGeeks" , "I" , "from" , "am" }; int n = arr.length; // Function to perform sorting Arrays.sort(arr, new Comparator<String>() { @Override public int compare( final String s1, final String s2) { return s1.length() < s2.length() ? - 1 : 1 ; } }); // Calling the function to print result printArrayString(arr, n); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Function to check the small string function compare(s1, s2) { return s1.length - s2.length; } // Function to print the sorted array of string function printArraystring(str, n) { for (let i = 0; i < n; i++) document.write(str[i] + " " ); } // Driver function let arr = [ "GeeksforGeeks" , "I" , "from" , "am" ] let n = arr.length // Function to perform sorting arr.sort(compare); // Calling the function to print result printArraystring(arr, n); // This code is contributed by gfgking. </script> |
Python3
from functools import cmp_to_key # Function to check the small string def compare(s1,s2): return len (s1) - len (s2) # Function to print the sorted array of string def printArraystring( str ,n): for i in range (n): print ( str [i],end = " " ) # Driver function arr = [ "GeeksforGeeks" , "I" , "from" , "am" ] n = len (arr) # Function to perform sorting arr.sort(key = cmp_to_key(compare)) # Calling the function to print result printArraystring(arr, n) # This code is contributed by shinjanpatra. |
输出
I am from GeeksforGeeks
方法#2:在python中使用sorted()函数简化解决方案
- 以字符串作为列表。
- 在python中,通过提供key as len来使用sorted函数。
以下是实施情况:
Python3
# Python code for the above approach def printsorted(arr): # Sorting using sorted function # providing key as len print ( * sorted (arr, key = len )) # Driver code arr = [ "GeeksforGeeks" , "I" , "from" , "am" ] # Passing list to printsorted function printsorted(arr) # this code is contributed by vikkycirus |
输出
I am from GeeksforGeeks
本文由 里沙布·贾因 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END