📌  相关文章
📜  按降序对字符串进行排序的程序

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

给定一个字符串,按降序对其进行排序。
例子:

Input : alkasingh
Output : snlkihgaa 

Input : nupursingh
Output : uusrpnnihg 

Input : geeksforgeeks
Output : ssrokkggfeeee 

一个简单的解决方案是使用库排序函数std::sort()

C++
// CPP program to sort a string in descending
// order using library function
#include 
using namespace std;
 
void descOrder(string s)
{
    sort(s.begin(), s.end(), greater());
}
 
int main()
{
    string s = "geeksforgeeks";
    descOrder(s); // function call
    return 0;
}


Java
// Java program to sort a string in descending
// order using library function
import java.util.*;
 
class GFG
{
 
    static void descOrder(char[] s)
    {
        Arrays.sort(s);
        reverse(s);
    }
 
    static void reverse(char[] a)
    {
        int i, n = a.length;
        char t;
        for (i = 0; i < n / 2; i++)
        {
            t = a[i];
            a[i] = a[n - i - 1];
            a[n - i - 1] = t;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        char[] s = "geeksforgeeks".toCharArray();
        descOrder(s); // function call
        System.out.println(String.valueOf(s));
    }
}
 
// This code is contributed by 29AjayKumar


Python
# Python program to sort
# a string in descending
# order using library function
 
def descOrder(s):
    s.sort(reverse = True)
    str1 = ''.join(s)
    print(str1)
 
def main():
    s = list('geeksforgeeks')
     
    # function call
    descOrder(s)
 
if __name__=="__main__":
    main()
 
# This code is contributed by
# prabhat kumar singh


C#
// C# program to sort a string in descending
// order using library function
using System;
     
class GFG
{
  
    static void descOrder(char[] s)
    {
        Array.Sort(s);
        reverse(s);
    }
  
    static void reverse(char[] a)
    {
        int i, n = a.Length;
        char t;
        for (i = 0; i < n / 2; i++)
        {
            t = a[i];
            a[i] = a[n - i - 1];
            a[n - i - 1] = t;
        }
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        char[] s = "geeksforgeeks".ToCharArray();
        descOrder(s); // function call
        Console.WriteLine(String.Join("",s));
    }
}
 
// This code is contributed by Rajput-Ji


PHP


Javascript


C++
// C++ program to sort a string of characters
// in descending order
#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 < str.length(); i++)
 
        // '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[str[i] - 'a']++;
 
    // Traverse the hash array and print
    // characters
    for (int i = MAX_CHAR - 1; i >= 0; i--)
        for (int j = 0; j < charCount[i]; j++)
            cout << (char)('a' + i);
}
 
// Driver program to test above function
int main()
{
    string s = "alkasingh";
    sortString(s);
    return 0;
}


Java
// Java program to sort a string of characters
// in descending order
 
class GFG
{
 
    static int MAX_CHAR = 26;
 
    // function to print string in sorted order
    static void sortString(String str)
    {
         
        // Hash array to keep count of characters.
        // Initially count of all charters is
        // initialized to zero.
        int charCount[] = new int[MAX_CHAR];
 
        // Traverse string and increment
        // count of characters
        // 'a'-'a' will be 0, 'b'-'a' will be 1,
        for (int i = 0; i < str.length(); i++)
        {
             
            // so for location of character in count
            // array we wil do str[i]-'a'.
            charCount[str.charAt(i) - 'a']++;
        }
 
        // Traverse the hash array and print
        // characters
        for (int i = MAX_CHAR - 1; i >= 0; i--)
        {
            for (int j = 0; j < charCount[i]; j++)
            {
                System.out.print((char) ('a' + i));
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "alkasingh";
        sortString(s);
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program to sort a string of characters
# in descending order
 
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]*MAX_CHAR;
 
    # Traverse string and increment
    # count of characters
    for i in range(len(str)):
 
        # '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(MAX_CHAR - 1,-1, -1):
        for j in range(charCount[i]):
            print(chr(97+i),end="");
 
# Driver program to test above function
s = "alkasingh";
sortString(s);
 
# This code is contributed by Princi Singh


C#
// C# program to sort a string of characters
// in descending order
using System;
 
class GFG
{
    static int MAX_CHAR = 26;
 
    // function to print string in sorted order
    static void sortString(String str)
    {
         
        // Hash array to keep count of characters.
        // Initially count of all charters is
        // initialized to zero.
        int []charCount = new int[MAX_CHAR];
 
        // Traverse string and increment
        // count of characters
        // 'a'-'a' will be 0, 'b'-'a' will be 1,
        for (int i = 0; i < str.Length; i++)
        {
             
            // so for location of character in 
            // count array we wil do str[i]-'a'.
            charCount[str[i] - 'a']++;
        }
 
        // Traverse the hash array and print
        // characters
        for (int i = MAX_CHAR - 1; i >= 0; i--)
        {
            for (int j = 0; j < charCount[i]; j++)
            {
                Console.Write((char) ('a' + i));
            }
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String s = "alkasingh";
        sortString(s);
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:

ssrokkggfeeee 

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

C++

// C++ program to sort a string of characters
// in descending order
#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 < str.length(); i++)
 
        // '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[str[i] - 'a']++;
 
    // Traverse the hash array and print
    // characters
    for (int i = MAX_CHAR - 1; i >= 0; i--)
        for (int j = 0; j < charCount[i]; j++)
            cout << (char)('a' + i);
}
 
// Driver program to test above function
int main()
{
    string s = "alkasingh";
    sortString(s);
    return 0;
}

Java

// Java program to sort a string of characters
// in descending order
 
class GFG
{
 
    static int MAX_CHAR = 26;
 
    // function to print string in sorted order
    static void sortString(String str)
    {
         
        // Hash array to keep count of characters.
        // Initially count of all charters is
        // initialized to zero.
        int charCount[] = new int[MAX_CHAR];
 
        // Traverse string and increment
        // count of characters
        // 'a'-'a' will be 0, 'b'-'a' will be 1,
        for (int i = 0; i < str.length(); i++)
        {
             
            // so for location of character in count
            // array we wil do str[i]-'a'.
            charCount[str.charAt(i) - 'a']++;
        }
 
        // Traverse the hash array and print
        // characters
        for (int i = MAX_CHAR - 1; i >= 0; i--)
        {
            for (int j = 0; j < charCount[i]; j++)
            {
                System.out.print((char) ('a' + i));
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "alkasingh";
        sortString(s);
    }
}
 
// This code is contributed by 29AjayKumar

蟒蛇3

# Python program to sort a string of characters
# in descending order
 
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]*MAX_CHAR;
 
    # Traverse string and increment
    # count of characters
    for i in range(len(str)):
 
        # '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(MAX_CHAR - 1,-1, -1):
        for j in range(charCount[i]):
            print(chr(97+i),end="");
 
# Driver program to test above function
s = "alkasingh";
sortString(s);
 
# This code is contributed by Princi Singh

C#

// C# program to sort a string of characters
// in descending order
using System;
 
class GFG
{
    static int MAX_CHAR = 26;
 
    // function to print string in sorted order
    static void sortString(String str)
    {
         
        // Hash array to keep count of characters.
        // Initially count of all charters is
        // initialized to zero.
        int []charCount = new int[MAX_CHAR];
 
        // Traverse string and increment
        // count of characters
        // 'a'-'a' will be 0, 'b'-'a' will be 1,
        for (int i = 0; i < str.Length; i++)
        {
             
            // so for location of character in 
            // count array we wil do str[i]-'a'.
            charCount[str[i] - 'a']++;
        }
 
        // Traverse the hash array and print
        // characters
        for (int i = MAX_CHAR - 1; i >= 0; i--)
        {
            for (int j = 0; j < charCount[i]; j++)
            {
                Console.Write((char) ('a' + i));
            }
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String s = "alkasingh";
        sortString(s);
    }
}
 
// This code is contributed by PrinciRaj1992

Javascript


输出:

snlkihgaa

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