How To Sort A String In C++ | 2 Different Ways

Sorting is very important in Competitive Programming. Here, we are going to discuss How to sort a string in C++ in 2 different ways. Sorting String refers to Sorting Multiple String or Sorting characters of One String. Anyway, I will discuss sorting of the one string according to their characters.

What is Sorting?

Before Begin with Sorting let’s have a look on Sorting Definition

Arranging Particular set in Given Order is Known As Sorting

Sorting refers to arranging, However, It Does not refer to only Increasing or decreasing order. Arranging according to Given Order or condition refers to Sorting.

Different Ways To Sort String In C++

There are basically two ways of Sorting in C++, both are given below.

• Using Sorting Techniques
• Using C++ STL Library

Sorting Techniques are basically Bubble Sort, Insertion Sort, Merge Sort and other Sorting Techniques whereas C++ STL library refers to use sort() function which is discussed in detail.

Using Sorting Techniques

There are multiple Sorting Techniques like Bubble sorting, merge Sorting, Quick Sorting. But In this post I am going to discuss Bubble sort.

Bubble Sortingto Sort Stringin C++:

This is one of the simplest sorting algorithm of sorting. What this algorithm states that,

Compare two Adjacent element and swap according to given condition

Let’s take an example you have a string named “GEEKS“. Now I am going to perform the Bubble Sorting in “GEEKS“.

1. We compare [G E E K S] and changed it to [E G E K S], We swap G with E because E comes before G
2. Again We compare [E G E K S] and changed it to [E E G K S]
3. Now Comparing [E E G K S] but we can not swap G and K because they are in the right order already.
4. We again compare [ E E G K S] but again we can’t swap because K and S are in right order

After all this, we get S as our Greatest Character in terms of position in Alphabets. I know this is sorted character but bubble sort did not know that.

Now Bubble Sort again works and Find out the second most greater character and this is done for an n-1 time where n is the number of characters in the string.

After all phase of Sorting we will get [E E G K S]

Program For Sorting all characters of One Single String

We used Bubble sorting for sorting all characters in string in bubblesort Function

``````#include <iostream>
#include <string>
using namespace std;

// A function to implement bubble sort
void bubbleSort(string str, int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)

if (str[j] > str[j+1])
{

char temp = str[j+1];
str[j+1] = str[j];
str[j] = temp;
}
cout<<"\n String after sorting: "<<str<<" \n";
}
int main ()
{
string str;
cout<<"Enter the string to be sorted: ";
cin>>str;
int len = str.length();
cout<<len;
cout<<"\n String before sorting: "<<str<<" \n";

bubbleSort(str,len);

return 0;
}``````

And the Output is given below.

`````` Enter the string to be sorted: 5
String before sorting: GEEKS

String after sorting: EEGKS
``````

Note: Above program only work if String is only in Capital Letter or Small Letter

Using In Build Sort Function

In this method, we use the inbuild function present in C++ STL for sorting known as Sort. Sort function uses the Quick Sort algorithm as their algorithm technique. The sort function is very easy to use and also perform searching operation fast.

Below is the implementation of sort function

Syntax Of Sort

``sort(starting address, ending address);``

Program to Sort String in C++ Using Sort method

``````#include <bits/stdc++.h>
using namespace std;

int main ()
{
string str;
cout<<"Enter String"<<endl; // Assume Entered string is "geekstocode"
cin>>str;
sort(str.begin(),str.end());
cout<<"Sorted String "<<endl;
cout<<str;

return 0;
}``````

Here we use str.begin() which gives the address of str[0] and str.end() which gives the last address of the string. Hereby default sort function sorts the string in ascending order and the output is

``````
cdeeegkoost``````

If you want to sort the string in descending order, then after sorting you can create a new string and copy the resultant string from the last index. This is how to sort a string in C++.

Problem Set Related to String Sorting

Sort Array Of String

Top 20 String Interview Problem

C Equal String

Count Binary String

Reverse Engineering Problem

getline in C

1. frolep rotrem says:

I just couldn’t depart your website prior to suggesting that I extremely enjoyed the standard info an individual supply for your guests? Is gonna be again ceaselessly in order to check out new posts

2. graphical says:

Yօu could definitely see your expeгtіse within the
article you write. The arena hоpes for more pаssionate writers
such as you who are not afraid to mention hoѡ thеу belіeve.
All the time ɡo after youг heaгt.

3. Arden Marander says:

Thank You

4. Socjologia Nauka says:

This is a appealing article by the way. I am going to go ahead and bookmark this post for my sister to read later on tonight. Keep up the good work.

5. WWW.XMC.PL says:

This is positively a weblog that people need to get behind. The problem is, no one wants to do a huge deal of reading and not have something else to stimulate the mind. This is the world wide web, after all. Maybe if you added a video or two to emphasise your point. Ill stick around, FOR SURE. But, I dont know if others will.