Javatpoint Logo
Javatpoint Logo

Print All Permutations in Sorted (Lexicographic) Order in C++

In this article, you will learn how to print all permutations in sorted order in C++ with its example. But before going to its implementation, you must know about the permutation and lexicographic order in C++.

What are Permutations?

A fundamental idea in computer science and combinatory is permutations. These are sets of items arranged in a certain sequence, and a common challenge in algorithm design is to determine all possible permutations of a set of elements.

What is Lexicographic Order?

Lexicographic order is an arrangement of the elements according to their alphabetical order; it is often referred to as dictionary order or alphabetical order. As it pertains to permutations, Lexical order places them in the same order as words in a dictionary.

  • For instance, take into consideration the set {A, B, C}.
  • The permutations are listed in the following lexicographic order:
    1. ABC
    2. ACB
    3. BAC
    4. BCA
    5. CAB
    6. CBA
  • The goal is to produce all possible character combinations for a string of length n in a sorted order.
  • Consider the following example to further grasp the issue:
    • Enter "XYZ"
    • XYZ, XZY, YXZ, YZX, ZXY, ZYX are the outputs.
  • In this case, all permutations must be printed in lexicographical order, which is an alphabetically increasing order.

The sorted array is the initial element of the permutation; thus, sorting it alphabetically in ascending order is the first step in solving this problem. After that, it produces the string's subsequent higher-level permutation.

You can better understand the solution by looking at the code below:

Output:

Print All Permutations in Sorted (Lexicographic) Order in C++

Code Explanation:

The provided C++ code creates and outputs, in lexicographic sequence, every variation of a given string. The reasoning employed in the code is explained as follows:

  • When sorting, characters are compared using the compare function. It is a common comparison function from the C Standard Library that can be used with qsort.
  • In the string, two characters can be switched using the swap function.
  • Within a given range of the string, the finduBound function locates the rightmost character that is smaller than a particular character first. This function is used to determine which character in the string should be swapped out for the character at position i.
  • After sorting the input text using qsort to place it in lexicographical order, the generatePermutation function creates and prints every permutation in the sorted order.
  • "NOPQ" is an example string that is fed into the main function.
  • The message printed by the main function indicates that permutations will be printed in sorted order.
  • After that, the function generatePermutation is called, passing in the input text "NOPQ".

Generating permutations frequently while keeping them in lexicographic order is the fundamental logic of the code. In order to achieve the next permutation, characters are swapped, and the string is kept ordered the entire time. The cycle keeps going until every possible combination is created and printed. The output shows how well the code generates, in lexicographic sequence, every conceivable variation of the supplied string.

Example Program:

Output:

Print All Permutations in Sorted (Lexicographic) Order in C++

Code Explanation:

  • Using std::sort, we first sort the input string in lexicographical order.
  • After that, we create and report permutations using std::next_permutation using a do-while loop. This function produces the next one until there are no more lexicographically bigger permutations to be formed.
  • The current permutation that we output inside the loop will be sorted in order because of the first sorting step.
  • With the input "XYZ", this code will output the possibilities in lexicographic order when it is run.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA