📜  字符的字符串排序

📅  最后修改于: 2021-10-27 07:08:45             🧑  作者: Mango

给定来自 ‘a’ – ‘z’ 的字符串小写字符。我们需要编写一个程序来按排序顺序打印这个字符串的字符。
例子:

Input : bbccdefbbaa 
Output : aabbbbccdef

Input : geeksforgeeks
Output : eeeefggkkorss

一种简单的方法是使用排序算法,如快速排序或合并排序,并对输入字符串排序并打印出来。

C++
// C++ program to sort a string of characters
#include
using namespace std;
 
// function to print string in sorted order
void sortString(string &str)
{
   sort(str.begin(), str.end());
   cout << str;
}
 
// Driver program to test above function
int main()
{
    string s = "geeksforgeeks";
    sortString(s);
    return 0;
}


Java
// Java program to sort a string of characters
 
import java.util.Arrays;
 
class GFG {
 
// function to print string in sorted order
    static void sortString(String str) {
        char []arr = str.toCharArray();
        Arrays.sort(arr);
        System.out.print(String.valueOf(arr));
    }
 
// Driver program to test above function
    public static void main(String[] args) {
        String s = "geeksforgeeks";
        sortString(s);
    }
}
// This code is contributed by Rajput-Ji


Python3
# Python3 program to sort a string
# of characters
 
# function to print string in
# sorted order
def sortString(str) :
    str = ''.join(sorted(str))
    print(str)
 
# Driver Code
s = "geeksforgeeks"
sortString(s)
 
# This code is contributed by Smitha


C#
// C# program to sort a string of characters
using System;   
public class GFG {
 
// function to print string in sorted order
    static void sortString(String str) {
        char []arr = str.ToCharArray();
        Array.Sort(arr);
        Console.WriteLine(String.Join("",arr));
    }
 
// Driver program to test above function
    public static void Main() {
        String s = "geeksforgeeks";
        sortString(s);
    }
}
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program to sort a string of characters
#include
using namespace std;
 
const int MAX_CHAR = 26;
 
// function to print string in sorted order
void sortString(string &str)
{
    // Hash array to keep count of characters.
    // Initially count of all charters is
    // initialized to zero.
    int charCount[MAX_CHAR] = {0};
     
    // Traverse string and increment
    // count of characters
    for (int i=0; i


Java
// Java program to sort
// a string of characters
public class SortString{
    static final int MAX_CHAR = 26;
 
    // function to print string in sorted order
    static void sortString(String str) {
 
        // Hash array to keep count of characters.
        int letters[] = new int[MAX_CHAR];
 
        // Traverse string and increment
        // count of characters
        for (char x : str.toCharArray()) {
 
            // 'a'-'a' will be 0, 'b'-'a' will be 1,
            // so for location of character in count
            // array we will do str[i]-'a'.
            letters[x - 'a']++;
        }
 
        // Traverse the hash array and print
        // characters
        for (int i = 0; i < MAX_CHAR; i++) {
            for (int j = 0; j < letters[i]; j++) {
                System.out.print((char) (i + 'a'));
            }
        }
    }
 
    // Driver program to test above function
    public static void main(String[] args) {
        sortString("geeksforgeeks");
    }
}
// This code is contributed
// by Sinuhe


Python3
# Python 3 program to sort a string
# of characters
 
MAX_CHAR = 26
 
# function to print string in sorted order
def sortString(str):
     
    # Hash array to keep count of characters.
    # Initially count of all charters is
    # initialized to zero.
    charCount = [0 for i in range(MAX_CHAR)]
     
    # Traverse string and increment
    # count of characters
    for i in range(0, len(str), 1):
         
        # 'a'-'a' will be 0, 'b'-'a' will be 1,
        # so for location of character in count
        # array we wil do str[i]-'a'.
        charCount[ord(str[i]) - ord('a')] += 1
     
    # Traverse the hash array and print
    # characters
    for i in range(0, MAX_CHAR, 1):
        for j in range(0, charCount[i], 1):
            print(chr(ord('a') + i), end = "")
 
# Driver Code
if __name__ == '__main__':
    s = "geeksforgeeks"
    sortString(s)
     
# This code is contributed by
# Sahil_Shelangia


C#
// C# program to sort
// a string of characters
using System;
 
class GFG
{
     
    // Method to sort a
    // string alphabetically
    public static string sortString(string inputString)
    {
         
        // convert input
        // string to char array
        char[] tempArray = inputString.ToCharArray();
 
        // sort tempArray
        Array.Sort(tempArray);
 
        // return new sorted string
        return new string(tempArray);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        string inputString = "geeksforgeeks";
 
        Console.WriteLine(sortString(inputString));
    }
}
 
// This code is contributed by Shrikant13


Javascript


输出:

eeeefggkkorss

时间复杂度: O(n log n),其中 n 是字符串的长度。
一种有效的方法是首先观察总共只能有 26 个唯一字符。因此,我们可以将从 ‘a’ 到 ‘z’ 的所有字符出现的次数存储在一个散列数组中。散列数组的第一个索引将代表字符“a”,第二个将代表“b”,依此类推。最后,我们将简单地遍历散列阵列,并从“A”到“Z”,他们在输入字符串出现的次数打印字符。
下面是上述想法的实现:

C++

// C++ program to sort a string of characters
#include
using namespace std;
 
const int MAX_CHAR = 26;
 
// function to print string in sorted order
void sortString(string &str)
{
    // Hash array to keep count of characters.
    // Initially count of all charters is
    // initialized to zero.
    int charCount[MAX_CHAR] = {0};
     
    // Traverse string and increment
    // count of characters
    for (int i=0; i

Java

// Java program to sort
// a string of characters
public class SortString{
    static final int MAX_CHAR = 26;
 
    // function to print string in sorted order
    static void sortString(String str) {
 
        // Hash array to keep count of characters.
        int letters[] = new int[MAX_CHAR];
 
        // Traverse string and increment
        // count of characters
        for (char x : str.toCharArray()) {
 
            // 'a'-'a' will be 0, 'b'-'a' will be 1,
            // so for location of character in count
            // array we will do str[i]-'a'.
            letters[x - 'a']++;
        }
 
        // Traverse the hash array and print
        // characters
        for (int i = 0; i < MAX_CHAR; i++) {
            for (int j = 0; j < letters[i]; j++) {
                System.out.print((char) (i + 'a'));
            }
        }
    }
 
    // Driver program to test above function
    public static void main(String[] args) {
        sortString("geeksforgeeks");
    }
}
// This code is contributed
// by Sinuhe

蟒蛇3

# Python 3 program to sort a string
# of characters
 
MAX_CHAR = 26
 
# function to print string in sorted order
def sortString(str):
     
    # Hash array to keep count of characters.
    # Initially count of all charters is
    # initialized to zero.
    charCount = [0 for i in range(MAX_CHAR)]
     
    # Traverse string and increment
    # count of characters
    for i in range(0, len(str), 1):
         
        # 'a'-'a' will be 0, 'b'-'a' will be 1,
        # so for location of character in count
        # array we wil do str[i]-'a'.
        charCount[ord(str[i]) - ord('a')] += 1
     
    # Traverse the hash array and print
    # characters
    for i in range(0, MAX_CHAR, 1):
        for j in range(0, charCount[i], 1):
            print(chr(ord('a') + i), end = "")
 
# Driver Code
if __name__ == '__main__':
    s = "geeksforgeeks"
    sortString(s)
     
# This code is contributed by
# Sahil_Shelangia

C#

// C# program to sort
// a string of characters
using System;
 
class GFG
{
     
    // Method to sort a
    // string alphabetically
    public static string sortString(string inputString)
    {
         
        // convert input
        // string to char array
        char[] tempArray = inputString.ToCharArray();
 
        // sort tempArray
        Array.Sort(tempArray);
 
        // return new sorted string
        return new string(tempArray);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        string inputString = "geeksforgeeks";
 
        Console.WriteLine(sortString(inputString));
    }
}
 
// This code is contributed by Shrikant13

Javascript


输出:

eeeefggkkorss

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程