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();
}
}
}
What happens?
– Reiksiel
Hello. What error do you find? What output did you expect? Please note that
b=int.Parse(Console.ReadLine())
is wrong. TheConsole.ReadLine()
can return anull
what causes aFormatException
in theint.Parse(...)
. The correct is to read the input for a variable, check that it is notnull
and then do theint.Parse
(orint.TryParser(...)
that does not return exceptions but rather a flag indicating whether Parsing was succeeded or not.– Omni
The sorting function is not even called in the main code, in addition to other small problems.
– Bacco
@Omni, or use
Nullable<Int32>
.– ptkato