c - Lexicographic order permutation -


i trying work on getting program print out numbers 1-4 in lexicographic order. able permute array of ints.

#include <stdio.h>  #define len 4  void swap(int array[], int i, int j); void permute(int array[], int low, int len);  int main() {   int array[len], i;    (i = 0; < len; i++)     array[i] = + 1;    permute(array, 0, len); }   void permute(int array[], int low, int len) {   int i;    if (low >= len - 1 ) {     (i = 0; < len; i++)       printf("%d ", array[i]);     printf("\n");   }   else {     (i = low; < len; i++) {       swap(array, low, i);       permute(array, low+1 , len );       swap(array, low, i);     }   } }   void swap(int array[], int i, int j) {   int temp = array[i];   array[i] = array[j];   array[j] = temp; } 

i have used recursion within program , swap methods swap numbers. explanation of how approach or example great.

example be:

this output

1234  1243  1324  1342  1432 .... 

this output should be

1234  1243  1324  1342  1423 .... 

notice how last number different output.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -