The code sorts a vector of up to 50,000 values from a txt
and stored in an array by quicksort
, but for 60,000 values or more, gives Overflow. I have already changed values to Int64 and it persists ... (obs: I change the size of the% vector% to the amount of values before executing)
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
class myclass
{
static void Main()
{
int counter = 0, aux = 0, aux2 = 0, flag = 0;
string line;
Int64[] a = new Int64[10000];
Stopwatch sw = new Stopwatch();
sw.Start();
System.IO.StreamReader file = new System.IO.StreamReader("C:\Users\Marco\Desktop\test1.txt");
while ((line = file.ReadLine()) != null)
{
a[counter] = Convert.ToInt32(line);
counter++;
}
file.Close();
Console.WriteLine("Iniciando ordenação");
Ordenar(a);
Console.WriteLine("Ordenação finalizada!");
sw.Stop();
Console.WriteLine("Quantidade de itens: " + counter);
Console.WriteLine("Tempo: " + sw.Elapsed);
do
{
Console.WriteLine("Deseja exibir o vetor?");
Console.WriteLine("1[sim] ou 2[não]");
aux2 = Convert.ToInt32(Console.ReadLine());
if (aux2 == 1)
{
do
{
Console.WriteLine(a[aux]);
aux++;
} while (aux < counter);
flag = 1;
Console.ReadLine();
}
else if (aux2 == 2)
{
flag = 1;
Console.ReadLine();
}
else
{
Console.WriteLine("opção incorreta!");
}
} while (flag != 1);
}
public static void Ordenar(Int64[] vetor)
{
Ordenar(vetor, 0, vetor.Length - 1);
}
private static void Ordenar(Int64[] vetor, Int64 inicio, Int64 fim)
{
if (inicio < fim)
{
Int64 posicaoPivo = Separar(vetor, inicio, fim);
Ordenar(vetor, inicio, posicaoPivo - 1);
Ordenar(vetor, posicaoPivo + 1, fim);
}
}
private static Int64 Separar(Int64[] vetor, Int64 inicio, Int64 fim)
{
Int64 pivo = vetor[inicio];
Int64 i = inicio + 1, f = fim;
while (i <= f)
{
if (vetor[i] <= pivo)
i++;
else if (pivo < vetor[f])
f--;
else
{
Int64 troca = vetor[i];
vetor[i] = vetor[f];
vetor[f] = troca;
i++;
f--;
}
}
vetor[inicio] = vetor[f];
vetor[f] = pivo;
return f;
}
}