Sort the given string using character search
Given a string str of size n. The problem is to sort the given string without using any sorting techniques (like bubble, selection, etc). The string contains only lowercase characters.
Examples:
Input : geeksforgeeks
Output : eeeefggkkorss
Input : coding
Output : cdgino
Algorithm:
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
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
string sortString(string str, int n) {
string new_str = "" ;
for ( int i = 'a' ; i <= 'z' ; i++)
for ( int j = 0; j < n; j++)
if (str[j] == i)
new_str += str[j];
return new_str;
}
int main() {
string str = "geeksforgeeks" ;
int n = str.size();
cout << sortString(str, n);
return 0;
}
|
Java
class GFG {
static String sortString(String str, int n)
{
String new_str = "" ;
for ( int i = 'a' ; i <= 'z' ; i++)
for ( int j = 0 ; j < n; j++)
if (str.charAt(j) == i)
new_str += str.charAt(j);
return new_str;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.length();
System.out.print(sortString(str, n));
}
}
|
Python3
def sortString( str , n):
new_str = ""
for i in range ( ord ( 'a' ), ord ( 'z' ) + 1 ):
for j in range (n):
if ( str [j] = = chr (i)):
new_str + = str [j]
return new_str
str = "geeksforgeeks"
n = len ( str )
print (sortString( str , n))
|
C#
using System;
class GFG {
static String sortString(String str, int n)
{
String new_str = "" ;
for ( int i = 'a' ; i <= 'z' ; i++)
for ( int j = 0; j < n; j++)
if (str[j] == i)
new_str += str[j];
return new_str;
}
public static void Main()
{
String str = "geeksforgeeks" ;
int n = str.Length;
Console.Write(sortString(str, n));
}
}
|
Javascript
<script>
function sortString(str, n) {
var new_str = "" ;
for ( var i = 'a' .charCodeAt(0); i <= 'z' .charCodeAt(0); i++)
for ( var j = 0; j < n; j++)
if (str[j].charCodeAt(0) == i)
new_str += str[j];
return new_str;
}
var str = "geeksforgeeks" ;
var n = str.length;
document.write( sortString(str, n));
</script>
|
Time complexity : O(n)
Auxiliary Space : O(1)
Method 2:
In the above method we have to traverse the entire string every time for each of the character in set of ‘a’ to ‘z’.We can overcome this drawback by maintaining a character and filling it with number of the occurrence’s of all the characters in the string.Later we can construct the required sorted string from the character array.
Below is the implementation.
C++
#include <iostream>
using namespace std;
string sortString(string str, int n) {
int i;
char arr[26]={0};
string new_str = "" ;
for (i = 0; i < n; i++)
++arr[str[i]- 'a' ];
for (i=0;i<26;i++)
while (arr[i]--)
new_str += i+ 'a' ;
return new_str;
}
int main() {
string str = "geeksforgeeks" ;
int n = str.size();
cout << sortString(str, n);
return 0;
}
|
Java
class GFG
{
static String sortString(String str, int n)
{
int i;
char [] arr = new char [ 26 ];
String new_str = "" ;
for (i = 0 ; i < n; i++)
++arr[str.charAt(i) - 'a' ];
for (i = 0 ; i < 26 ; i++)
while (arr[i]-- > 0 )
{
new_str += String.valueOf(( char )(i + 'a' ));
}
return new_str;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.length();
System.out.print(sortString(str, n));
}
}
|
Python3
def sortString(st, n):
arr = [ 0 ] * 26
new_str = ""
for i in range (n):
arr[ ord (st[i]) - ord ( 'a' )] + = 1
for i in range ( 26 ):
while (arr[i] > 0 ):
new_str + = chr (i + ord ( 'a' ))
arr[i] - = 1
return new_str
if __name__ = = "__main__" :
st = "geeksforgeeks"
n = len (st)
print (sortString(st, n))
|
C#
using System;
class GFG
{
static String sortString(String str, int n)
{
int i;
char [] arr = new char [26];
String new_str = "" ;
for (i = 0; i < n; i++)
++arr[str[i] - 'a' ];
for (i = 0; i < 26; i++)
while (arr[i]-- > 0)
{
new_str += String.Join( "" ,( char )(i + 'a' ));
}
return new_str;
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.Length;
Console.Write(sortString(str, n));
}
}
|
Javascript
<script>
function sortString(str, n) {
var i;
var arr = Array(26).fill(0);
var new_str = "" ;
for (i = 0; i < n; i++)
++arr[str[i].charCodeAt(0) - 'a' .charCodeAt(0)];
for (i=0;i<26;i++)
while (arr[i]--)
new_str += String.fromCharCode(i + 'a' .charCodeAt(0));
return new_str;
}
var str = "geeksforgeeks" ;
var n = str.length;
document.write( sortString(str, n));
</script>
|
Time complexity : O(n)
Auxiliary Space : O(1)
Last Updated :
04 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...