Simple permutation algorithm

3

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();
        }
    
asked by anonymous 31.01.2016 / 00:22

1 answer

4

Since you are using modernities and ready-made functions that do the work for you, you do not have to complicate so much:

public static int Teste(int number) {
    return number >= 10000000 ? -1 : ToInt32(new string(number.ToString().OrderByDescending(x => x).ToArray()));
}

See running on dotNetFiddle .

Note that I've separated rendering from the presentation, so it's more organized.

If you were to use the original code, you would have to fix the error. But the error occurred because the code was too complicated. As it was simplifying, it would be easy to find the error, or simplification would already eliminate the error.

I note that this code had several inconsistencies. In addition it used a non-idiomatic pattern for C #. It would be easy to eliminate ref and mainly out in the helper function.

You would not need to create a list to generate as many, if any, a List<char> " and not a ArrayList that should no longer be used in any code.

You have too many variables in the code, duplicating the data from one variable to another without any need, meaningless ties.

If I were to use a manual algorithm, I would start making one from scratch in a simpler way.

    
31.01.2016 / 01:26