Stackoverflowexception for more than 50,000 values

Asked

Viewed 62 times

2

The code sorts an array of up to 50,000 values from a txt and stored in a array for quicksort, but for 60,000 values or more, it’s Overflow. I’ve changed values to Int64 and persist... (Obs: I change the vector size a for 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;
    }
}
  • StackOverflowException means that you are popping the stack (it usually occurs in recursions, as it is a case), not that a variable is receiving a value that exceeds its data domain. I’m too rusty in C# to give an answer, but try putting ref Int64[] vetor in the declaration of duties Ordenar and Separar. I don’t remember exactly the behavior of C# in relation to arrays.

  • In Split I can make the modification, but in Sort it gives an error stating that there are invalid methods inside Sort

  • 3

    Solved! the problem is in reading txt, modifying the mode of receiving the values, it works.

  • Cool! Happy birthday!

  • 5

    This question seems to be discounted because it is about a problem unrelated to the original question, already discovered and corrected by the author.

No answers

Browser other questions tagged

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