algorithms – lexicographic permutation list

/ * list all the permutations of the first n positive integers * /

/ * in the lexicographic order. * /

typedef int Bool;

in one[N + 1], not;

void step_in (void);

void next_permutation (const int left_index, const int right_index);

void print_permutation (void);

void get_n (void);

void initialize_a (void);

main ()

{

get_n ();

initialize_a ();

act ();

returns 0;

}

/ ************************************************ * ********

  • step_in: this recursive function manipulates the *

  • values ​​of left_index, right_index and *

  • direction. These variables control the *

  • general flow of the algorithm, determinant *

  • if a permutation is to be calculated or *

  • printed and if the function must be *

  • went out. *

************************************************** ******* /

empty step_in (void)

{

static enum {LEFT, RIGHT} direction = RIGHT;

static left_index = 0;

int right_index; / * successive calls of step_in hide * /

/ * previous values ​​of right_index * /

for (;;) {

if (direction == RIGHT) {

++ left_index;

right_index = n;

if (left_index == n – 1) {

print_permutation ();

next_permutation (left_index, right_index);

print_permutation ();

direction = LEFT;

Pause;

}

else if (left_index <n – 1)

act ();

other {

/ * special cases where n is 0 or 1 are treated here * /

print_permutation ();

Pause;

}

}

other {

–left_index;

if (left_index <right_index) {

next_permutation (left_index, right_index);

–right_index;

direction = RIGHT;

act ();

}

other

Pause;

}

}

}

/ ************************************************ * ********

  • next_permutation: switch the elements of the array a *

  • between them to get a new *

  • permutation of these elements. *

************************************************** ******* /

void next_permutation (const int left_index, const int right_index)

{

int i;

int temp[N + 1];

int top = left_index;

temp[top] = one[right_index];

++ top;

for (i = n; i> right_index; top ++, i–)

temp[top] = one[i];

temp[top] = one[left_index];

++ top;

for (i = right_index – 1; i> left_index; top ++, i–)

temp[top] = one[i];

for (i = left_index; i <= n; i ++)

a[i] = temp[i];

}

/ ************************************************ * ********

  • print_permutation: displays the current permutation. *

************************************************** ******* /

void print_permutation (void)

{

int i;

printf (" n ("));

if (n> 0) {

printf ("% 2d", a1);

for (i = 2; i <= n; i ++)

printf (",% 2d", a[i])

}

printf (") n n");

}

/ ************************************************ * ********

  • get_n: prompt the user to specify the permutation length n. Read all *

  • characters entered by the user up to the new line *

  • character. Repeat if invalid data or *

  • Illegal permutation length is entered. *

************************************************** ******* /

get_n (empty)

{

Bool invalid_int_val;

char next_char;

for (;;) {

invalid_int_val = FALSE;

printf (" n");

printf ("Enter n:");

scanf ("% d", & n);

for(;;) {

if ((next_char = getchar ()) == & # 39; n & # 39;)

Pause;

else if (next_char! = & # 39; && next_char! = & # 39; t & # 39;)

invalid_int_val = TRUE;

for (; getchar ()! = & # 39; n & # 39 ;;)

;

Pause;

}

}

if (invalid_int_val || n < 0 || n > NOT) {

printf (" n");

printf ("The number n must be an integer between% d and% d.
again. n ", 0, N);

}

other

Pause;

}

}

/ ************************************************ * ********

  • initialize_a: initialize array a. *

************************************************** ******* /

void initialize_a (void)

{

int i;

for (i = 1; i <= n; i ++)

a[i] = i;

}