Updated April 19, 2023
Introduction to Sort string C++
Sorting a string is defined as string arrangement either in ascending or descending order or any given order is known as sorting in C++ which is nothing but getting the strings given in proper order or given order can be said as the strings are sorted in the given or specified arrangement. In C++, there are different ways of sorting string such as ascending or increasing order, descending or decreasing order where it can be done using sorting techniques (bubble sort, merge sort, insertion sort) or using STL libraries in C++.
Working of sorting string in C++
In C++, sorting string is done using two ways one with using some of the sorting techniques and another to use in-built STL Library that is provides by C++. Sorting strings is just as arranging the given strings in a specified order such as ascending order or descending order. Now let us in detail in the below section.
Sorting string using a few sorting techniques
In C++, different sorting techniques are available for string sorting. Let us discuss a few in the below section with examples.
1. Bubble Sort
In C++, bubble sort is one of the easiest sorting techniques. In this sorting technique, the strings are sorted by comparing the adjacent strings or characters in the string and swap them according to the specified order that can be alphabetically in the case of strings in C++.
Example:
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
void sortStrings(char arr[][MAX], int n)
{
char temp[MAX];
for (int j=0; j<n-1; j++)
{
for (int i=j+1; i<n; i++)
{
if (strcmp(arr[j], arr[i]) > 0)
{
strcpy(temp, arr[j]);
strcpy(arr[j], arr[i]);
strcpy(arr[i], temp);
}
}
}
}
int main()
{
char arr1[][MAX] = {"Educba","Institute","Technology","Python","India","Asia"};
int a = sizeof(arr1)/sizeof(arr1[0]);
sortStrings(arr1, a);
printf(" Demonstration of string sorting using Bubble sort in C++");
printf("\n");
printf("Strings in sorted order are : ");
for (int i=0; i<a; i++)
printf("\n String %d is %s", i+1, arr1[i]);
return 0;
}
Output:
In the above program, we can see we have already declared a set of strings in an array and then we are comparing adjacent strings and swapping them using temp variable. Then we are arranging the given array in an alphabetical order which is done using bubble sort. The output can be seen in the above screenshot.
2. Insertion Sort
This is also another simple sorting technique in C++ where the strings given in an array are compared with each other sequentially which means the sorted array can be obtained by comparing one string at a time where the array can be divided into sorted and unsorted sub-arrays and then the strings in the unsorted array are arranged in the correct order. Then the array is sorted in alphabetical order.
Example:
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 7
#define MAX_STRING_LEN 200
void StringInsertionSort(char list[MAX_STRINGS][MAX_STRING_LEN]);
int main()
{
int i;
char strings[MAX_STRINGS][MAX_STRING_LEN];
printf(" Demonstration of string sorting using Insertion sort");
printf("\n");
printf("Enter %d strings.\n", MAX_STRINGS);
for (i = 0; i < MAX_STRINGS; i++)
{
printf("String entered in index is %d : ", i);
scanf("%199s", strings[i]); // limit the width so we don't go past the buffer
strings[i][sizeof(strings[i]) - 1] = '\0';
}
StringInsertionSort(strings);
printf("\nThe entered string in sorted order using insertion sort are:\n");
for (i = 0; i < MAX_STRINGS; i++)
{
printf("%s\n", strings[i]);
}
}
void StringInsertionSort(char list[MAX_STRINGS][MAX_STRING_LEN])
{
for (int a = 1; a < MAX_STRINGS; a++)
{
int b = a;
while (b > 0 && strcmp(list[b - 1], list[b]) > 0)
{
char tmp[MAX_STRING_LEN];
strncpy(tmp, list[b - 1], sizeof(tmp) - 1);
tmp[sizeof(tmp) - 1] = '\0';
strncpy(list[b - 1], list[b], sizeof(list[b - 1]) - 1);
list[b - 1][sizeof(list[b - 1]) - 1] = '\0';
strncpy(list[b], tmp, sizeof(list[b]));
list[b][sizeof(list[b]) - 1] = '\0';
--b;
}
}
}
Output:
In the above program, we can see we are using user input for entering a string and then arranging the strings. In the above code, we can see we are comparing the strings in sequential order using temp variable and then arranging them in alphabetical order. The output can be seen in the above screenshot.
Many other sorting techniques in C++ also can be used for sorting string such as quicksort, merge sort and in insertion sort only there are again different ways such as binary insertion sort, etc.
- Sorting string using C++ STL sort()
Let us see sorting a string using C++ STL library that is sort() function which is provided by the library in C++ which is included in <algorithm> header.
Example:
#include<bits/stdc++.h>
using namespace std;
void StringSort(string &str1)
{
sort(str1.begin(), str1.end());
cout << "The sorted string using sort() function is " << endl;
cout << str1;
cout << endl;
}
int main()
{
cout << " Demonstration of sorting strig using C++ STL library sort() is as follows:" << endl;
cout << " \n " << endl;
string s = "EducbaInstituteOfTechnologyIndia";
cout << " The given string is " << endl;
cout << s;
cout << endl;
StringSort(s);
return 0;
}
Output:
In the above program, we can see Firstly we have created a function named StringSort() in which w are defining the sort() built-in function where we can see the string that is declared as an argument to the StrinSort() function is str1, then when we are we want to sort the given string then we have to start it traversing from the first character till the last so in the sort() function with syntax as sort(starting address, ending address) we can see str1.begin() will store the first letter and str1.end() will store the last character of the string and the sorting is done alphabetically by comparing each character in the given string. The output can be seen in the above screenshot.
Let us another example that will sort string in descending order using the same std: :sort() function.
Example:
#include <iostream>
#include <algorithm>
using namespace std;
bool comparator(string &p, string &q){
return p > q;
}
int main() {
string arr1[] = {"Black", "Red", "Blue", "Yellow", "White", "Purple"};
int size = 6;
cout << "Demonstration of sorting strings using sort()" << endl;
std::sort(arr1, arr1 + size, comparator);
for (int a = 0; a < 6; a++){
cout<<arr1[a]<<endl;
}
return 0;
}
Output:
In the above program, we are using the sort() function defined by <algorithm> and we are arranging the given strings in a descending order using sort(). The output is as seen in the above screenshot.
Conclusion
In this article, we conclude that the string sorting in C++ is done either sing sorting techniques such as bubble sort, insertion sort, merge sort, quick sort, etc the developers need to check for the time complexity also for the proper and quick sorting of the given string. Another way of sorting string is using C++ STL library such as <algorithm> header for using built-in sort() function for sorting string in alphabetical order either in ascending or descending order.
Recommended Articles
This is a guide to Sort string C++. Here we discuss the Working of sorting string in C++ with examples and Sorting string using a few sorting techniques. You may also have a look at the following articles to learn more –