I am trying to write several different sorting algorithms in order to time the differences between them when reading a file of a half million integers. For testing purposes, I am only using a few hundred integers, but I am getting a stack overflow error. Also, for testing reasons, I have an output file being created during this run so that I can see if the code is working correctly. Originally, I had 3 nested for loops that I believed were causing the errors (and horrible effeciencey), but I cleaned those up and I am still having the error. Can someone look this over and see if there is a glaring issue that I am overlooking. Thanks for any help you can give. I will also upload the .txt file of integers, in case anyone feels up to compiling this to recreate the error. I am using VS2012 Ultimate for my editing/compiling.
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
void quickSort(int a[], int first, int last);
int pivot(int a[], int first, int last);
void swap(int& a, int& b);
using namespace std;
int main()
{
const int SIZE = 500000;
string s[SIZE];
int test[SIZE];
int i = 0;
int N = sizeof(test)/sizeof(int);
ifstream readFile;
ofstream myOutputFile;
int start_s=clock(); //start timer
readFile.open("inputData.txt");
if (readFile.is_open())
{
while (!readFile.eof())
{
if (i < SIZE) //for all values in the inputData read file
{
getline(readFile, s[i]); //store values in a string array
test[i] = atoi(s[i].c_str()); //convert string array to integer array
i++;
}
}
}
quickSort(test, 0, SIZE-1);
readFile.close(); //close file
int stop_s=clock();//stop timer
cout <<"time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;//output timer
myOutputFile.open("sortedValues.txt");
for (int i =0; i < SIZE; i++)
myOutputFile << test[i] << endl;
myOutputFile.close();
return 0;
system("pause");
}
/*
* Recursive Quicksort algorithm.
*/
void quickSort( int a[], int first, int last )
{
int pivotElement;
if(first < last)
{
pivotElement = pivot(a, first, last);
quickSort(a, first, pivotElement-1);
quickSort(a, pivotElement+1, last);
}
}
/*
* Find and return the index of pivot element.
*/
int pivot(int a[], int first, int last)
{
int p = first;
int pivotElement = a[first];
for(int i = first+1 ; i <= last ; i++)
{
if(a[i] <= pivotElement)
{
p++;
swap(a[i], a[p]);
}
}
swap(a[p], a[first]);
return p;
}
/*
* Swap the parameters.
*/
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}