
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Sort Elements in Lexicographical Order (Dictionary Order)
Lexicographical order denotes the way the words are ordered in a list, based on alphabetical order according to their alphabets. For example −
List of words: Harry Adam Sam Lexicographical order of words: Adam Harry Sam
A program to sort elements in lexicographical order is as follows −
Example
#include <iostream> using namespace std; int main() { int i,j; string s[5], temp; cout<<"Enter the elements..."<<endl; for(i = 0; i < 5; ++i) getline(cin, s[i]); for(i = 0; i < 4; ++i) for(j = i+1; j < 5; ++j) { if(s[i] > s[j]) { temp = s[i]; s[i] = s[j]; s[j] = temp; } } cout << "The elements in lexicographical order are... " << endl; for(int i = 0; i < 5; ++i) cout << s[i] << endl; return 0; }
Output
The output of the above program is as follows −
Enter the elements… Orange Grapes Mango Apple Guava The elements in lexicographical order are... Apple Grapes Guava Mango Orange
In the above program, string s[] is defined and the elements are entered by the user. This is given below −
string s[5], temp; cout<<"Enter the elements..."<<endl; for(i = 0; i < 5; ++i) getline(cin, s[i]);
The elements are arranged alphabetically by using nested for loops. The code snippet for this is as follows −
for(i = 0; i < 4; ++i) for(j = i+1; j < 5; ++j) { if(s[i] > s[j]) { temp = s[i]; s[i] = s[j]; s[j] = temp; } }
Finally all the elements in lexicographical order are displayed. This is given below −
cout << "The elements in lexicographical order are... " << endl; for(int i = 0; i < 5; ++i) cout << s[i] << endl;
- Related Articles
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Swift Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Kotlin Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Haskell Program to Sort Elements in Lexicographical Order (Dictionary Order)
- How to Sort Elements in Lexicographical Order (Dictionary Order) in Golang?
- Sort the words in lexicographical order in C#
- Sort the words in lexicographical order in Java
- Sort the words in lexicographical order in Python
- C# Program to order array elements in descending order
- C++ Program to Sort the Elements of an Array in Descending Order
- C++ Program to Sort the Elements of an Array in Ascending Order
- Last Substring in Lexicographical Order in C++
- Sort list elements in descending order in C#
- Java Program to Sort the Array Elements in Descending Order
- C program to sort names in alphabetical order

Advertisements