I can’t call the function

Asked

Viewed 245 times

0

I made that code Quick Sort below and I can’t call the function. So far I’ve only done basic functions and can’t find the error.

public static void ordenar(int[] vetor)
{
    ordenar(vetor, 0, vetor.Length - 1);
}
public static void ordenar(int[] vetor, int inicio, int fim)
{
    if (inicio < fim)
    {
        int posisaoPivo=separa(vetor, inicio, fim);
        ordenar(vetor, inicio, posisaoPivo - 1);
        ordenar(vetor, posisaoPivo + 1, fim);
    }
}
private static int  separa(int[] vetor, int inicio, int fim)
{
    int pivo = vetor[inicio];
    int i = inicio + 1, f = fim;
    while (i <= f)
    {
        if (vetor[i] <= pivo)
            i++;
        else if (pivo < vetor[f])
            f--;
        else
        {
            int troca = vetor[i];
            vetor[i] = vetor[f];
            vetor[f] = troca;
            i++;
            f--;
        }
    }
    vetor[inicio] = vetor[f];
    vetor[f] = pivo;
    return f;
}

Core programme:

static void Main(string[] args)
        {

            int a=0, b=0,c=0;
            Console.WriteLine("digite os numeros");
            int[] numeros = new int[4];
           b=int.Parse(Console.ReadLine());
           c=int.Parse(Console.ReadLine());


            for (int i = 0; i < 4; i++)
            {
                a = separa(numeros,b,c);

              numeros[i] = int.Parse(Console.ReadLine()); 
            }


            Console.WriteLine("numeros {0}=",separa(numeros, b, c).ToString());
            Console.ReadKey();
        }

    }
}
  • 2

    What happens?

  • 1

    Hello. What error do you find? What output did you expect? Please note that b=int.Parse(Console.ReadLine()) is wrong. The Console.ReadLine() can return a null what causes a FormatException in the int.Parse(...). The correct is to read the input for a variable, check that it is not null and then do the int.Parse (or int.TryParser(...) that does not return exceptions but rather a flag indicating whether Parsing was succeeded or not.

  • 1

    The sorting function is not even called in the main code, in addition to other small problems.

  • @Omni, or use Nullable<Int32>.

1 answer

1

If what you are trying to do is test the sorting algorithm, see if the code below helps you.

using System;


class Program
    {
        static void Main(string[] args)
        {
            int[] vetor = new int[0];

            int index = 1;
            while (true)
            {
                Console.Write("Didite o " + index + "° numero ou 'enter' para ordenar:");
                string entrada = Console.ReadLine();
                if (entrada == "")
                    break;

                int num = 0;
                if(int.TryParse(entrada, out num))
                {
                    Array.Resize(ref vetor, index);
                    vetor[index-1] = num;
                    index++;
                }
            }


            Console.WriteLine("Vetor informado:");
            foreach (int i in vetor)
            {
                Console.Write(i.ToString() + " ");
            }

            Ordenar(vetor);

            Console.WriteLine("");
            Console.WriteLine("Vetor ordenado:");
            foreach (int i in vetor)
            {
                Console.Write(i.ToString() + " ");
            }

            Console.WriteLine("");
            Console.WriteLine("Pressione 'Enter' para sair.");
            Console.ReadLine();

        }



        public static void Ordenar(int[] vetor)
        {
            Ordenar(vetor, 0, vetor.Length - 1);
        }

        private static void Ordenar(int[] vetor, int inicio, int fim)
        {
            if (inicio < fim)
            {
                int posicaoPivo = Separar(vetor, inicio, fim);
                Ordenar(vetor, inicio, posicaoPivo - 1);
                Ordenar(vetor, posicaoPivo + 1, fim);
            }
        }

        private static int Separar(int[] vetor, int inicio, int fim)
        {
            int pivo = vetor[inicio];
            int i = inicio + 1, f = fim;
            while (i <= f)
            {
                if (vetor[i] <= pivo)
                    i++;
                else if (pivo < vetor[f])
                    f--;
                else
                {
                    int troca = vetor[i];
                    vetor[i] = vetor[f];
                    vetor[f] = troca;
                    i++;
                    f--;
                }
            }
            vetor[inicio] = vetor[f];
            vetor[f] = pivo;
            return f;
        }

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.