
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort an Array of Strings in Lexicographical Order
In this problem, we will sort the array of strings in the lexicographical order.
There are various sorting algorithms to sort the array of strings. In this tutorial, we will use the built?in sort() method to sort the strings. Also, we will use the merge sort algorithm, one of the efficient approaches to sort the array of strings.
Problem statement ? We have given an str[] array containing N strings. We need to sort all strings in the lexicographical order.
Sample examples
Input
str[] = {"tutorials", "point", "save", "java", "c++"}
Output
c++, java, point, save, tutorials
Explanation ? We sorted the string in lexicographical order.
Input
str[] = {"aad", "aab", "aaa", "aac", "cpp"};
Output
aaa, aab, aac, aad, cpp
Input
str[] = {"ztbq", "ytrc", "ppp", "ppp", "ppp"};
Output
ppp, ppp, ppp, ytrc, ztbq
Approach 1
In this approach, the sort() method of C++ is used to sort the array of strings.
Example
In the example below, we pass the ?str' string as the first parameter of the sort() method and str + len as the second parameter of the sort() method. It takes the reference to the starting and ending index as a parameter.
After that, we print all strings of the array. In the output, we can observe the sorted array of strings.
#include <bits/stdc++.h> using namespace std; void sortElements(int len, string str[]) { // Sort strings sort(str, str + len); for (int p = 0; p < len; p++) { cout << str[p] << endl; } } int main() { string str[] = {"tutorials", "point", "save", "java", "c++"}; int len = sizeof(str) / sizeof(str[0]); sortElements(len, str); return 0; }
Output
c++ java point save tutorials
Time complexity ? O(NlogN)
Space complexity ? O(logN) due to recursive calls of the quick sort.
Approach 2
In this approach, we will use the merge sort algorithm to sort the array of strings in the lexicographical order. The merge sort works based on the divide and conquer strategy. We recursively divide the array into two parts and sort both parts while merging them into the actual array.
Algorithm
Step 1 ? If the ?left' is less than the ?right,' follow the below steps. We get the ?left' and ?right' as a parameter.
Step 2 ? Take the middle index.
Step 3 ? Call the sortStrings() function for the left and right parts separately.
Step 4 ? Finally, execute the mergeArray() function to merge the left and right parts after sorting.
Step 4.1 ? In the mergeArray() function, initialize the ?leftLen' with the length of the left part and ?rightLen' with the length of the right part of the array. Also, define the ?leftArr' of the ?leftLen' length and ?rightArr' of the ?rightLen' length.
Step 4.2 ? Initialize the leftArr and rightArr using the original's array's elements.
Step 4.3 ? Initialize the p, q with 0, and K with the left.
Step 4.4 ? Make iterations while p is less than leftLen and q is less than right Len.
Step 4.5 ? If leftArr[p] is less than rightArr[q], update str[k] with the leftArr[p], and increment p by 1. Otherwise, update str[k] with the rightArr[q] and increment q by 1. Also, increment the k value.
Step 4.6 ? Add the remaining elements of the leftArr[] to the str[] array. Also, add the remaining elements of the rightArr[] to the str[] array.
Example
#include <bits/stdc++.h> using namespace std; void mergeArray(string str[], int left, int mid, int right) { int leftLen = mid - left + 1; int rightLen = right - mid; string leftArr[leftLen], rightArr[rightLen]; // Store string in temmporary array for (int p = 0; p < leftLen; p++) leftArr[p] = str[left + p]; for (int q = 0; q < rightLen; q++) rightArr[q] = str[mid + 1 + q]; int p = 0; int q = 0; int k = left; // sort and merge while (p < leftLen && q < rightLen) { // Update str array with sorted elements if (leftArr[p] <= rightArr[q]) { str[k] = leftArr[p]; p++; } else { str[k] = rightArr[q]; q++; } k++; } // Append remaining elements of leftArr[] while (p < leftLen) { str[k] = leftArr[p]; p++; k++; } // Append remaining elements of rightArr[] while (q < rightLen) { str[k] = rightArr[q]; q++; k++; } } void sortStrings(string str[], int left, int right) { if (left < right) { // Get middle index int mid = left + (right - left) / 2; // Sort the left half sortStrings(str, left, mid); // Sort the right half sortStrings(str, mid + 1, right); // Merge both halves mergeArray(str, left, mid, right); } } int main() { string str[] = {"tutorials", "point", "save", "java", "c++"}; int arrLen = sizeof(str) / sizeof(str[0]); sortStrings(str, 0, arrLen - 1); cout << "\nSorted array is - "; for (int p = 0; p < arrLen; p++) cout << str[p] << " "; return 0; }
Output
Sorted array is ? c++ java point save tutorials
Time complexity ? O(NlogN) to sort each part of the array.
Space complexity ? O(N) to store elements in the temporary array.
We used the sort() method and merge sort algorithm to sort the array of strings. The sort() method combines heap, quick, and insertion sort to make the sorting algorithm faster. However, programmers may use the bubble sort or selection sort to sort the array of strings.