Methods to Sort Strings in C++
Sort strings in C++
“In C++, sorting a string is defined as arranging strings in ascending or descending order or any provided order. There are various methods for sorting strings in C++, including ascending or growing order and descending or decreasing order. These methods may use STL libraries instead of sorting methods (such as bubble sort, merge Sort, and insertion sort).”
In C++, there are two ways to sort strings: one uses various sorting methods, and the other uses the STL Library, which is part of C++. Sorting strings involves placing strings in a particular order, such as ascending or descending.
1. Bubble sort
Steps for sorting string using the bubble sort method:
- Take the string's first element as a starting point.
- Compare the chosen element to the subsequent element.
- Consider exchanging the two items or moving on to the element after the chosen one if it is larger.
- Repeat these actions until the array is sorted and the chosen element is at the end of it.
Code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char str[50], t;
int flag =0, count=0;
printf("Please enter the string that should be sorted:\n");
scanf("%s", str);
int sl=strlen(str);
for(int i=0; i<sl-1; i++){
for(int j=i+1; j<sl; j++){
count++;
if(str[i]>str[j]){
t = str[i];
str[i] = str[j];
str[j] = t;
}
}
}
printf("\nYour Sorted string is %s:", str);
return 0;
}
Output:
![‘;Methods to Sort Strings in C++](https://static.tutorialandexample.com/cpp/methods-to-sort-strings-in-cpp1.png)
2. Quick Sort
Steps for sorting strings using the Quick sort method:
- Take the string's first element as a starting point.
- Compare the chosen element to the subsequent element.
- Consider exchanging the two items or moving on to the element after the chosen one if it is larger.
- Follow these steps repeatedly until the selected element is at the end of the array and the array is sorted.
Code:
#include<stdio.h>
#include<string.h>
void swap (char *p, char *q)
{
int t = *p;
*p = *q;
*q = t;
}
int partition (char array[], int bottom, int top)
{
int Pivot = array[top];
int i = (bottom - 1);
for (int j = bottom; j <= top - 1; j++)
{
if (array[j] <= Pivot)
{
i++;
swap (&array[i], &array[j]);
}
}
swap (&array[i + 1], &array[top]);
return (i + 1);
}
void quickSort (char array[], int bottom, int top)
{
if (bottom < top)
{
int PI = partition (array, bottom, top);
quickSort (array, bottom, PI - 1);
quickSort (array, PI + 1, top);
}
}
void printArray (char array[])
{
printf ("%s ", array);
}
int main ()
{
char array[30] ;
printf("Please enter the string that should be sorted:\n");
scanf("%s", array);
int l = strlen(array);
quickSort (array, 0, l - 1);
printf ("Your Sorted Array is: \n");
printArray (array);
return 0;
}
Output:
![‘;Methods to Sort Strings in C++](https://static.tutorialandexample.com/cpp/methods-to-sort-strings-in-cpp2.png)
3. Merge Sort
Steps for sorting string using the Merge sort method:
- The string's middle index should be found.
- From the center, divide the string.
- To use the mergesort technique, only the first half of the string must be supplied.
- The second half of the string should be held with the same method.
- Combine the sorted components to get the full sorted ring.
Code:
#include <stdio.h>
#include<string.h>
#define MAX 1000
char array_1[MAX];
char array_2[MAX];
void merge(int bottom, int Middle, int top)
{
int i, j, k;
for(i=bottom, j=Middle+1, k=bottom; i<=Middle && j<=top; k++)
{
if(array_1[i]<array_2[j])
{
array_2[k]=array_1[i];
i++;
}
else
{
array_2[k] = array_1[j];
j++;
}
}
while(i<=Middle)
{
array_2[k]=array_1[i];
k++;
i++;
}
while(j<=top)
{
array_2[k]=array_1[j];
k++;
j++;
}
for (int l = 0; l <top+1 ; ++l)
{
array_1[l]=array_2[l];
}
}
void mergesort(int bottom, int top)
{
if(bottom<top)
{
int Middle= (bottom + top)/2;
mergesort(bottom,Middle);
mergesort(Middle+1, top);
merge(bottom, Middle, top);
}
else
return;
}
int main()
{
int sl;
printf("Please enter the string: \n" );
scanf("%s", array_1);
sl= strlen(array_1);
printf("\nSorted String is : ");
mergesort(0, sl-1);
printf("%s", array_1);
}
Output:
![‘;Methods to Sort Strings in C++](https://static.tutorialandexample.com/cpp/methods-to-sort-strings-in-cpp3.png)
4. Insertion Sort:
The characters are selected individually and put in the correct place using this simplest sort method. This approach removes a character from the input list and adds to the sorted sub-string after each iteration.
Steps for sorting strings using the insertion sort method:
- First, enter a string.
- Traverse the string using a for loop.
- Assume the first entry is a sorted sublist.
- Compare each element to the ones in the sorted sublist before deciding. All the larger parts should be moved to the right.
- Repeat steps 4-5 until the string is finished to obtain a sorted string.
Code:
//Sort string in CPP example using insertion method
#include <iostream>
#include <string>
using namespace std;
int main(){
string Str;
cout << "Please enter the string that should be sorted: ";
getline(cin, Str);
int l = Str.length();
cout << "\nThe prior sort string is: " << Str << " \n";
for (int i = 1; i < l; i++){
char temp = Str[i];
// Inserting s[j] at its appropriate position
int j = i - 1;
while (j >= 0 && Str[j] > temp){
Str[j + 1] = Str[j];
j--;
}
Str[j + 1] = temp;
}
cout << "\nThe after sort string is: " << Str << " \n";
return 0;
}
Output:
![‘;Methods to Sort Strings in C++](https://static.tutorialandexample.com/cpp/methods-to-sort-strings-in-cpp4.png)