Permutation of n-size array with possible repeated elements. E.g [1, 2, 1]
-
05-11-2019 - |
Pergunta
What would it be a recursive algorithm to get permutations for any list of n
elements that might contain or not repeated elements?
For the following 3-element list [1, 1, 2]
I would expect the following result:
[1, 1, 2]
[1, 2, 1]
[2, 1, 1]
So far I have the following result:
[1, 1, 2] <- duplicate
[1, 2, 1] <- duplicate
[1, 1, 2]
[1, 2, 1]
[2, 1, 1] <- duplicate
[2, 1, 1]
with algorithm below:
FUNCTION permute(array, nestingLevel) :
FOR index = nestingLevel TO array size -1
SWAP array[index] WITH array[nestingLevel]
CALL permute (array, nestingLevel + 1)
SWAP array[nestingLevel] WITH array[index]
END FOR
IF recursionNestingLevel EQUAL TO array size - 1
PRINT array
END IF
END FUNCTION
DEFINE array[] := 1, 1, 2
CALL permute (array, 0)
Nenhuma solução correta
Licenciado em: CC-BY-SA com atribuição
Não afiliado a cs.stackexchange