Stack overflow (parameters: 0x00000001, 0x00A02FAC)

-2

Unhandled exception at 0x007A9E89 in Project1.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00A02FAC).

the main idea from the code is creating a txt file that has a 100,000 number why I'm doing this it's project that I have to do, after the debug retch this function long int partition(long int T[], long int first, long int last) it's stop and gives me this error Unhandled exception at 0x00007FF71888A809 in Project1.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000002887403F60). I end up with changing the stack size from the default (1MB) to (100MB) as my doctor suggest but dose not change any thing, also I got the file I need but the problem is that I need the time and the other things so can you please help me.

#include<iostream> 
#include<ctime> 
#include<cstdlib> 
#include<fstream> 
using namespace std;
const long int n = 100000;//size of the array 
long int partition(long int arr[], long int first, long int last);
long int partition2(long int T[], long int first, long int last);
void quickSort1(long int array3[], long int low, long int high); 
void quickSort2(long int array3[], long int low, long int high);
double execution_stepin(long int array1[], long int n, int x);
void readfile(long int array1[]);
long int static iteration = 0;
     int main()
{
    ofstream ofile; 
    ofile.open("unsorted3.txt");
    long int i;
    for (i = n; i > 0; i--)
    {
        ofile << i << "\n";

    }
    ofile.close();

    long int array1[n];
    double total_time = 0;
    cout << "starting quick sort with descending order array values version1" << endl;
    cout << "   " << endl;
    readfile(array1);
    total_time += execution_stepin(array1, n, 0);
    readfile(array1); 
    total_time += execution_stepin(array1, n, 1);
    readfile(array1);
    total_time += execution_stepin(array1, n, 2);
    cout << "The average execution time is " << total_time / 3.0 << endl; 
    cout << "   " << endl;
    total_time = 0;

    cout << "starting quick sort with descending order array values version2" << endl;
    cout << "   " << endl; 
    readfile(array1);
    total_time += execution_stepin(array1, n, 3);
    readfile(array1);
    total_time += execution_stepin(array1, n, 4);
    readfile(array1); 
    total_time += execution_stepin(array1, n, 5);
    cout << "The average execution time is " << total_time / 3.0 << endl;
    cout << "   " << endl;
    ofstream ofile2; 
    ofile2.open("quick_sor_version3.txt");
    for (i = 0; i < n; i++)
    {
        ofile2 << array1[i] << "\n";
    }
    ofile2.close();


}
void readfile(long int array1[])
{
    ifstream ifile;
    long int next;
    ifile.open("unsorted3.txt");
    long int i = 0;
    while (!ifile.eof())
    {
        ifile >> next; 
        array1[i] = next; 
        i++;
    }
    ifile.close();
}
double execution_stepin(long int array1[], long int n, int x)
{
    time_t time1, time2; 
    clock_t start, end;
    double dif_sec;

    time(&time1);
    start = clock(); 
    if (x < 3)
        quickSort1(array1, 0, n);
    else
        iteration = 0;
    quickSort2(array1, 0, n);
    time(&time2); 
    end = clock();
    dif_sec = difftime(time2, time1);
    cout << " Execution no " << (x % 3) + 1 << endl;
    cout << "   " << endl;
    cout << "\nIt took " << dif_sec << " seconds to sort using quick sort" << endl;
    cout << "\ntime_t: start time " << time1 << " " << " end time " << time2 << " " << "Difference " << time2 -time1 << endl;
    cout << "\nclock_t: Execution time " << start << " " << end << " " << "time in seconds " << double(end - start) / CLOCKS_PER_SEC << endl;
    return (double(end - start) / CLOCKS_PER_SEC);
}
void quickSort1(long int T[], long int first, long int last)
{
    long int cut;
    if (first < last)
    {
        cut = partition(T, first, last);
        quickSort1(T, first, cut);
        quickSort1(T, cut + 1, last);

    }
}

long int partition(long int T[], long int first, long int last)


{
    long int pivot, temp;
    long int loop, cutPoint, left, right;
    pivot = T[first];
    left = first + 1; 
    right = last;
    loop = 1;   //always TRUE 
    while (loop) {
    while (T[left] < pivot && left <= last)
    {
        left++;
    }
    while (T[right] > pivot && right > left)
    {
        right--;
    }
    if (left < right)
    {
        temp = T[left]; T[left] = T[right];
        T[right] = temp; left++;
        right--;
    }
    if (left >= right)
    {
        temp = T[left - 1];
        T[left - 1] = T[first]; 
        T[first] = temp; 
        cutPoint = left - 1;
        loop = 0;
    }

}// end while
    return cutPoint;
}//end partition function
void quickSort2(long int T[], long int first, long int last)
{
    long int cut;

    iteration++; 
    if (iteration == 1)
    {
        if (first < last)
        {
            cut = partition2(T, first, last);
            if (cut == -1)
                return;
            quickSort1(T, first, cut);
            quickSort1(T, cut + 1, last);

        }
    }
    if (first < last)
    {
        cut = partition(T, first, last); 
        quickSort1(T, first, cut);
        quickSort1(T, cut + 1, last);

    }
}
long int partition2(long int T[], long int first, long int last)
{
    long int pivot, temp;
    long int loop, cutPoint, left, right; 
    pivot = T[first];
    left = first + 1; 
    right = last;
    loop = 1;   //always TRUE 
    long int order=0;
    while (loop) {
        while (T[left] < pivot && left <= last)
        {
            left++;
        }
        while (T[right] > pivot && right > left)
        {
            if (T[right] < T[right - 1]) order = 1;
            right--;
        }
        if (left < right)
        {
            temp = T[left];
            T[left] = T[right]; 
            T[right] = temp;
            left++;
            right--;
        }
        if (left >= right)
        {
            temp = T[left - 1];
            T[left - 1] = T[first];
            T[first] = temp;


            cutPoint = left - 1;
            loop = 0;
        }
        if (order == 0) 
            cutPoint = -1;
    }// end while 
    return cutPoint;
}//end par
c++
asked on Stack Overflow May 1, 2021 by alkadri24 • edited May 1, 2021 by alkadri24

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0