Sort string array based on length in C++
In this tutorial, We are going to learn how we can sort a string array based on the length of the individual strings in C++. Let us see some examples related to this problem.
for example : let us take an array of strings input which are arr[]={"I", "Love", "What about you", "Programming"} So now the output will become "I" "Love" "Programming" "What about you"
It can be solved by two approaches Namely:
- By Using Any Sorting Technique.
- By Using C++ STL Sort Function.
BY USING THE C++ STL SORT FUNCTION METHOD
Now to solve this problem we are using the STL Sort function in C++ for a Better Time complexity for the code to the solution.
For a detailed study follow this link Comparing sort(), partial_sort() and nth_element() sort() in C++ STL
Implementation of sort array based on string length for both orders (increasing and decreasing)
#include<bits/stdc++.h> using namespace std; // Function to check for the small string bool increasing(string str,string str2) { if(str.length()<str2.length()) return 1; else return 0; } // Driver Code int main() { string str1[]={"I","Love ","programming","what about you"}; int n = sizeof(str1)/sizeof(str1[0]); // Function to perform sorting sort(str1 , str1 + n , increasing); for(int i=0;i<n;i++) std::cout << str1[i] << std::endl; return 0; }
OUTPUT
I Love programming what about you
#include<bits/stdc++.h> using namespace std; // Function to check for the large string bool decreasing(string str, string str2) { if(str.length()>str2.length()) return 1; else return 0; } int main() { string str1[]={"I","Love ","programming","what about you"}; int n = sizeof(str1)/sizeof(str1[0]); // Function to perform sorting sort(str1 , str1 + n , decreasing); for(int i=0;i<n;i++) std::cout << str1[i] << std::endl; return 0; }
OUTPUT
what about you programming Love I
SORTING METHOD APPROACH
Here we can use any sorting technique such as Insertion sort, selection sort, bubble sort, etc. We are going to use the selection sorting approach for the sorting as initially choose the smallest component in the exhibit and swap it with the principal component in the cluster in each pass of the loop. As a result, We get the sorted array.
For a detailed study of the selection, sort refer to this link Selection Sort in C++
IMPLEMENTATION OF THE CODE
#include <bits/stdc++.h> using namespace std; // function to perform the sorting void selectionSort(char arr[][50], int n){ int i, j, mIndex; // Move the boundary condition of the unsorted array char minStr[50]; for (i = 0; i < n-1; i++){ // to find the minimum element int mIndex = i; strcpy(minStr, arr[i]); for (j = i + 1; j < n; j++){ //to compare whether min is greater than arr[j] if (strcmp(minStr, arr[j]) > 0){ // to make arr[j]= minStr and update the minimum index strcpy(minStr, arr[j]); mIndex = j; } } // swapping of the minimum and first element if (mIndex != i){ char temp[50]; strcpy(temp, arr[i]); strcpy(arr[i], arr[mIndex]); strcpy(arr[mIndex], temp); } } } //Driver function int main(){ char arr[][50] = {"l", "what about you", "programming" ,"love"}; int n = sizeof(arr)/sizeof(arr[0]); int i; // Function call for the sorting selectionSort(arr, n); for (i = 0; i < n; i++) cout << arr[i] << endl; return 0; }
OUTPUT
I Love Programming What about you
Leave a Reply