I'm trying to create a simple permutation algorithm where you pass any number eg 123 and it should return the largest number without repeating. I tried something of this type more when I step 4242 it does not return me 4422 but 4242 as larger.
static void ComparacoesPossiveis(ref int entrada, out int saida)
{
saida = 1;
for (int i = 1; i <= entrada; i++)
{
saida *= i;
}
}
public static void teste(int number)
{
if (number >= 10000000)
{
Console.WriteLine(-1);
Console.ReadKey();
return;
}
var array = number.ToString().ToArray();
int tamanhoArray = array.Length;
int[] vetor = new int[tamanhoArray];
int[] temp = new int[tamanhoArray];
int[] vetTmp = new int[tamanhoArray];
for (int i = 0; i < tamanhoArray; i++)
{
vetor[i] = Convert.ToInt32(array[i].ToString());
}
int y, x;
y = vetor.Length;
ArrayList combinacoes = new ArrayList();
ComparacoesPossiveis(ref y, out x);
while (x > 0)
{
for (int j = 0; j < y - 1; j++)
{
string numero = "";
vetTmp[j] = vetor[j];
vetor[j] = vetor[j + 1];
vetor[j + 1] = vetTmp[j];
for (int i = 0; i < vetor.Length; i++)
numero += vetor[i].ToString();
combinacoes.Add(Convert.ToInt32(numero));
}
x -= y;
}
combinacoes.Sort();
Console.WriteLine("O maior numero é " + combinacoes[combinacoes.Count - 1].ToString());
Console.ReadKey();
}