📜  使用字符搜索对给定的字符串进行排序

📅  最后修改于: 2021-04-24 21:31:45             🧑  作者: Mango

给定大小为n的字符串str 。问题是在不使用任何排序技术(例如冒泡,选择等)的情况下对给定的字符串进行排序。该字符串仅包含小写字符。

例子:

Input : geeksforgeeks
Output : eeeefggkkorss

Input : coding
Output : cdgino

算法:

sortString(str, n)
    Initialize new_str = ""
    
    for i = 'a' to 'z'
        for j = 0 to n-1
            if str[j] == i, then
                new_str += str[j]

    return new_str
C++
// C++ implementation to sort the given string without
// using any sorting technique
#include 
using namespace std;
  
// function to sort the given string without
// using any sorting technique
string sortString(string str, int n) {
  
  // to store the final sorted string
  string new_str = "";
  
  // for each character 'i'
  for (int i = 'a'; i <= 'z'; i++)
  
    // if character 'i' is present at a particular
    // index then add character 'i' to 'new_str'
    for (int j = 0; j < n; j++)
      if (str[j] == i)
        new_str += str[j];
  
  // required final sorted string
  return new_str;
}
  
// Driver program to test above
int main() {
  string str = "geeksforgeeks";
  int n = str.size();
  cout << sortString(str, n);
  return 0;
}


Java
// Java implementation to sort the given 
// string without using any sorting technique
class GFG {
      
    // function to sort the given string 
    // without using any sorting technique
    static String sortString(String str, int n)
    {
  
        // to store the final sorted string
        String new_str = "";
  
        // for each character 'i'
        for (int i = 'a'; i <= 'z'; i++)
  
            // if character 'i' is present at a 
            // particular index then add character
            // 'i' to 'new_str'
            for (int j = 0; j < n; j++)
                if (str.charAt(j) == i)
                    new_str += str.charAt(j);
  
        // required final sorted string
        return new_str;
    }
      
    // Driver code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        int n = str.length();
          
        System.out.print(sortString(str, n));
    }
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python3 implementation to sort 
# the given string without using
# any sorting technique
  
# Function to sort the given string 
# without using any sorting technique
def sortString(str, n):
  
    # To store the final sorted string
    new_str = ""
  
    # for each character 'i'
    for i in range(ord('a'), ord('z') + 1):
  
        # if character 'i' is present at a particular
        # index then add character 'i' to 'new_str'
        for j in range(n):
            if (str[j] == chr(i)):
                new_str += str[j]
  
    # required final sorted string
    return new_str
  
# Driver Code
str = "geeksforgeeks"
n = len(str)
print(sortString(str, n))
  
# This code is contributed by Anant Agarwal.


C#
// C# implementation to sort the given
// string without using any sorting technique
using System;
  
class GFG {
      
    // function to sort the given string
    // without using any sorting technique
    static String sortString(String str, int n)
    {
        // to store the final sorted string
        String new_str = "";
  
        // for each character 'i'
        for (int i = 'a'; i <= 'z'; i++)
  
            // if character 'i' is present at a
            // particular index then add character
            // 'i' to 'new_str'
            for (int j = 0; j < n; j++)
                if (str[j] == i)
                    new_str += str[j];
  
        // required final sorted string
        return new_str;
    }
  
    // Driver code
    public static void Main()
    {
        String str = "geeksforgeeks";
        int n = str.Length;
  
        Console.Write(sortString(str, n));
    }
}
  
// This code is contributed by Sam007


C++
// C++ implementation to sort the given
// string without using any sorting technique
#include  
using namespace std; 
  
string sortString(string str, int n) { 
int i;
//A character array to store the no.of occurances of each character
//between 'a' to 'z'
char arr[26]={0};
  
//to store the final sorted string 
string new_str = ""; 
  
//To store each occrance of character by relative indexing
for (i = 0; i < n; i++) 
   ++arr[str[i]-'a'];
  
  
//To traverse the character array and append it to new_str
for(i=0;i<26;i++)
  while(arr[i]--)
    new_str += i+'a'; 
  
return new_str; 
} 
  
// Driver program to test above 
int main() { 
string str = "geeksforgeeks"; 
int n = str.size(); 
cout << sortString(str, n); 
return 0; 
} 
  
// This code is contributed by Aravind Alapati


Java
// Java implementation to sort the given
// String without using any sorting technique
class GFG 
{
  
    static String sortString(String str, int n)
    {
        int i;
          
        // A character array to store 
        // the no.of occurances of each 
        // character between 'a' to 'z'
        char[] arr = new char[26];
  
        // to store the final sorted String
        String new_str = "";
  
        // To store each occrance of 
        // character by relative indexing
        for (i = 0; i < n; i++)
            ++arr[str.charAt(i) - 'a'];
  
        // To traverse the character 
        // array and append it to new_str
        for (i = 0; i < 26; i++)
            while (arr[i]-- > 0) 
            {
                new_str += String.valueOf((char)(i + 'a'));
            }
  
        return new_str;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        String str = "geeksforgeeks";
        int n = str.length();
        System.out.print(sortString(str, n));
    }
}
  
// This code is contributed by Rajput-Ji


C#
// C# implementation to sort the given
// String without using any sorting technique
using System;
  
class GFG 
{
  
    static String sortString(String str, int n)
    {
        int i;
          
        // A character array to store 
        // the no.of occurances of each 
        // character between 'a' to 'z'
        char[] arr = new char[26];
  
        // to store the readonly sorted String
        String new_str = "";
  
        // To store each occrance of 
        // character by relative indexing
        for (i = 0; i < n; i++)
            ++arr[str[i] - 'a'];
  
        // To traverse the character 
        // array and append it to new_str
        for (i = 0; i < 26; i++)
            while (arr[i]-- > 0) 
            {
                new_str += String.Join("",(char)(i + 'a'));
            }
  
        return new_str;
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        String str = "geeksforgeeks";
        int n = str.Length;
        Console.Write(sortString(str, n));
    }
}
  
// This code is contributed by 29AjayKumar


输出 :
eeeefggkkorss

方法2:
在上面的方法中,我们每次都要遍历整个字符串,即从’a’到’z’中的每个字符。我们可以通过维护一个字符并用所有字符的出现次数来填充来克服此缺点在字符串。稍后我们可以从字符数组构造所需的排序字符串。

下面是实现。

C++

// C++ implementation to sort the given
// string without using any sorting technique
#include  
using namespace std; 
  
string sortString(string str, int n) { 
int i;
//A character array to store the no.of occurances of each character
//between 'a' to 'z'
char arr[26]={0};
  
//to store the final sorted string 
string new_str = ""; 
  
//To store each occrance of character by relative indexing
for (i = 0; i < n; i++) 
   ++arr[str[i]-'a'];
  
  
//To traverse the character array and append it to new_str
for(i=0;i<26;i++)
  while(arr[i]--)
    new_str += i+'a'; 
  
return new_str; 
} 
  
// Driver program to test above 
int main() { 
string str = "geeksforgeeks"; 
int n = str.size(); 
cout << sortString(str, n); 
return 0; 
} 
  
// This code is contributed by Aravind Alapati

Java

// Java implementation to sort the given
// String without using any sorting technique
class GFG 
{
  
    static String sortString(String str, int n)
    {
        int i;
          
        // A character array to store 
        // the no.of occurances of each 
        // character between 'a' to 'z'
        char[] arr = new char[26];
  
        // to store the final sorted String
        String new_str = "";
  
        // To store each occrance of 
        // character by relative indexing
        for (i = 0; i < n; i++)
            ++arr[str.charAt(i) - 'a'];
  
        // To traverse the character 
        // array and append it to new_str
        for (i = 0; i < 26; i++)
            while (arr[i]-- > 0) 
            {
                new_str += String.valueOf((char)(i + 'a'));
            }
  
        return new_str;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        String str = "geeksforgeeks";
        int n = str.length();
        System.out.print(sortString(str, n));
    }
}
  
// This code is contributed by Rajput-Ji

C#

// C# implementation to sort the given
// String without using any sorting technique
using System;
  
class GFG 
{
  
    static String sortString(String str, int n)
    {
        int i;
          
        // A character array to store 
        // the no.of occurances of each 
        // character between 'a' to 'z'
        char[] arr = new char[26];
  
        // to store the readonly sorted String
        String new_str = "";
  
        // To store each occrance of 
        // character by relative indexing
        for (i = 0; i < n; i++)
            ++arr[str[i] - 'a'];
  
        // To traverse the character 
        // array and append it to new_str
        for (i = 0; i < 26; i++)
            while (arr[i]-- > 0) 
            {
                new_str += String.Join("",(char)(i + 'a'));
            }
  
        return new_str;
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        String str = "geeksforgeeks";
        int n = str.Length;
        Console.Write(sortString(str, n));
    }
}
  
// This code is contributed by 29AjayKumar
输出 :
eeeefggkkorss