Permutations( "12345" ) 

PROCEDURE Permutations ; 
( ; 
tvThings AS CHARACTER, ; 
tvStatic AS CHARACTER, ; 
tvN  AS INTEGER ; 
) AS VOID 
LOCAL iX AS INTEGER 
IF VARTYPE(tvStatic)="L" 
* First time procedure called, preset missing parameters. 
CLEAR 
PUBLIC lnPermutation AS INTEGER 
lnPermutation = 0 
tvStatic  = "" 
tvN  = LEN(tvThings) 
ENDIF 
FOR iX = 1 TO tvN 
IF tvN > 2 
Permutations( RIGHT( tvThings, tvN-1 ), tvStatic+LEFT(tvThings,1), tvN-1 ) 
ELSE 
lnPermutation = lnPermutation + 1 
? lnPermutation, tvStatic+tvThings 
ENDIF 
tvThings = SUBSTR(tvThings,2) + LEFT(tvThings,1) 
ENDFOR 
ENDPROC 




From: Tomás Corrales Lemoine 
Sent: Wednesday, September 14, 2011 4:15 PM
To: GUFA List Member 
Subject: [GUFA] Algoritmo para permutaciones


Hola colegas.

 

¿Alguien me podría ayudar con algún algoritmo para obtener todas las 
permutaciones posibles de un conjunto finito cualquiera? Por ejemplo, para el 
conjunto de 4 elementos ABCD las posibles permutaciones serían:

 

1.       ABCD

2.       ABDC

3.       ACBD

4.       ACDB

5.       ADBC

6.       ADCB

7.       BACD

8.       BADC

9.       BCAD

10.   BCDA

11.   BDAC

12.   BDCA

13.   CABD

14.   CADB

15.   CBAD

16.   CBDA

17.   CDAB

18.   CDBA

19.   DABC

20.   DACB

21.   DBAC

22.   DBCA

23.   DCAB

24.   DCBA

 

Es decir, la cantidad de permutaciones sería igual al factorial de la cantidad 
de elementos del conjunto. En este caso 4! = 4 x 3 x 2 x 1 = 24. Agradecería 
cualquier sugerencia. Gracias.

 

Saludos

 

Ing. Tomás Corrales Lemoine


-- 
Aeropuerto Internacional Frank País García de Holguín. 

Responder a