Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 379123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:51:40+00:00 2026-05-12T14:51:40+00:00

from Wikipedia : Lexicographical order generation For every number k, with 0 ≤ k

  • 0

from Wikipedia:

Lexicographical order generation

For every number k, with 0 ≤ k < n!,
the following algorithm generates the
corresponding lexicographical
permutation of the initial sequence
sj, j = 1, …, n:

function permutation(k, s) {
     var int n:= length(s); factorial:= 1;
     for j= 2 to n- 1 {             // compute (n- 1)!
         factorial:= factorial* j;
     }
     for j= 1 to n- 1 {
         tempj:= (k/ factorial) mod (n+ 1- j);
         temps:= s[j+ tempj]
         for i= j+ tempj to j+ 1 step -1 {
             s[i]:= s[i- 1];      // shift the chain right
         }
         s[j]:= temps;
         factorial:= factorial/ (n- j);
     }
     return s;
 }

What is the logic behind this? How does it work??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:51:40+00:00Added an answer on May 12, 2026 at 2:51 pm

    Imagine you have a whole number x and you want to know what digit is in the hundreds position. (E.g. if x=4723 you want the answer 7.) To calculate this, you first divide by 100, throwing away the fractional part. (In our example, this leaves 47.) Then find the remainder when you divide by 10.

    Now suppose you want to find the value of the digit in the thousands position. To find that you’d first divide by 1000, throwing away the fractional part, then again find the remainder when you divide by 10.

    In the regular decimal numbering system, each place holds one of 10 values. You can observe that in our digit-finding exercise we first divide by the number of possible combinations of values in places to the right of the one we care about (10 * 10 in the first example). Then we find the remainder when dividing by the number of possible values for the place we care about. Of course, all places have 10 possible values, so we just divide by 10.

    Now, imagine a numbering system where each place holds a different number of values. Our right-most place can have two values, 0 or 1. The next place can have three values, 0, 1 or 2; and so on. In this system we count like this:

      0
      1
     10
     11
     20
     21
    100
    101
    110
    111
    120
    121
    200
    201
    210
    211
    220
    ...
    

    This is what wrang-wrang means by a “variable-base number”.

    Now, you can see how we calculate the digit in a place in this system. To find the right-most we don’t need to divide first, and we find the remainder modulo 2, because there are 2 possible values for a digit in that column. To find the next column to the left, we first divide by the number of possible combinations for digits in the columns on the right: there’s only one column with two possible digits, so we divide by 2. Then we take the remainder modulo 3, because there are three possible values for this column. Continuing left, for the 3rd column we divide by 6 (because the columns to the right have 3 and 2 possibilities each, multiplying to make 6) and then take the remainder modulo 4, because there are 4 possible values in this column.

    Let’s take a look at the function:

    function permutation(k, s) {
        var int n:= length(s); factorial:= 1;
        for j= 2 to n- 1 {             // compute (n- 1)!
            factorial:= factorial* j;
        }
    

    factorial starts as (n-1)!

        for j= 1 to n- 1 {
    

    Each time we get here, factorial is equal to (n-j)! This is obvious the first time round, since j=1 and we know we initialized factorial to (n-1)! We’ll see later that factorial is indeed always (n-j)!

            tempj:= (k/ factorial) mod (n+ 1- j);
    

    Here we divide k by factorial (which is equal to (n-j)!) and throw away the remainder, then we take the remained when we divide the result by (n+1-j). Wait a minute, all that babble I started with is beginning to sound familiar! We are just finding the value of the “digit” in the nth column from the left using our “variable-base number system”!

    This next bit takes the sequence of elements between indices j and j + tempj and rotates it rightward – i.e. every element moves up one index, except the last one, which moves back to the start. It’s important to realise that all the numbers on the right of position j are in order. We’re effectively plucking one of them out and nudging the rest along to keep them in order. Which one we pluck out depends on tempj. When tempj is 0, we pick the smallest (and actually don’t need to do any nudging), when tempj is equal to n-j, we pick the largest.

            temps:= s[j+ tempj]
            for i= j+ tempj to j+ 1 step -1 {
                s[i]:= s[i- 1];      // shift the chain right
            }
            s[j]:= temps;
    

    Next, (n-j)! divided by (n-j) gives (n-j-1)!
    If you think about it, you should see that this means that when we get back to the top of the loop and j has incremented by one, factorial will once again equal (n-j)!

            factorial:= factorial/ (n- j);
        }
        return s;
    }
    

    I hope that helps a bit!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From Wikipedia: The complexity of the algorithm is O(n(logn)(loglogn)) bit operations. How do you
I have the following JSON string (from wikipedia http://en.wikipedia.org/wiki/JSON ) { name:Product, properties: {
I have the following batch script from Wikipedia: @echo off for /R C:\Users\Admin\Ordner %%f
I have the following batch script from Wikipedia: @echo off for /R C:\Users\Admin\Ordner %%f
The following definition from wikipedia explains what is the Data Access Layer in a
I am trying to find the Big O for stooge sort. From Wikipedia algorithm
The pseudocode as taken from Wikipedia: function Dijkstra(Graph, source): 2 for each vertex v
The following example is from Wikipedia . int arr[4] = {0, 1, 2, 3};
I've found this pseudocode from Wikipedia of Euclid's extended Algorithm, but I don't know
The following image is from wikipedia entry on call stack and there is something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.