Please help me (this would be very handy for a school project due tommorow)
I haven been trying to implement a recursive quick-sort algorithm in C++, however, when i run it i get a runtime segmentation fault(windows):
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
void getRandomList(int, int*);
void outputList(int, int*);
void quicksort(int, int*);
int main()
{
cout<<"Quick Sort example, By Jack Wilkie\n";
while (true)
{
cout<<"Please enter list length\n";
string pnput;
cin>>pnput;
cin.ignore(1);
stringstream temp;
temp << pnput;
int length;
temp >> length;
if (length < 1 || length > 100000)
{
cout<<"INVALID INPUT! (0 < input < 100,000)\n";
}
else
{
cout<<"Creating random list of "<<length<<" items\n";
int *list = new int[length];
getRandomList(length, list);
outputList(length, list);
double start = clock();
quicksort(length, list);
double stop = clock();
double time = stop-start;
cout<<time<<"ms";
cin.get();
delete[] list;
break;
}
}
}
void quicksort(int len, int* list)
{
if (len < 1)
{
return;
}
else
{
int low[10];
int mid[10];
lmid[0] = list[0];
int high[10];
int lens[3] = {0,1,0};
for(int i = 1; i < len; i++)
{
if(list[i] < list[0])
{
low[lens[0]] = list[i];
lens[0]++;
}
else if (list[i] > list[0])
{
high[lens[2]] = list[i];
lens[2]++;
}
else
{
mid[lens[1]] = list[i];
lens[1]++;
}
}
quicksort(lens[0], low);
quicksort(lens[2], high);
for(int i = 0; i < len; i++)
{
if (i < lens[0])
{
list[i] = low[i];
}
else if (i < lens[0]+lens[1])
{
list[i] = mid[i-lens[0]];
}
else
{
list[i] = high[i-lens[0]-lens[1]];
}
}
}
return;
}
My debug program (dev c++, almost out of internet and cant get anything big) say that the error is on line
lmid[0] = list[0];
however i cannot find any issues with it, it has the error as soon as it calls the quicksort function, i beleive the issue has something to do with passing an array, and i am fairly sure that the function isn't recursing and bloating the stack
in case you need it for debuging, here are the other functions i used
void getRandomList(int length, int* output)
{
for(int i = 0; i < length; i++)
{
output[i] = rand() % 100;
}
return;
}
void outputList(int length, int* input)
{
for(int i = 0; i < length; i++)
{
cout<<input[i]<<" ";
}
cout<<"\n";
return;
}