I have this function which does recursive selection sort for an array:
void SelectionSort::RecursiveSort(int ar[] , int flag, int first, int last){
// first - index of first element and last - index of last element
if (flag == 1)
{
if (first < last) //ascending
{
for (int i = first; i <= last; i++) if (ar[i] < ar[first]) swap(ar[i], ar[first]);
RecursiveSort(ar, flag, first + 1, last);
}
if (first == last) return;
}
else
{
if (first < last) //desc
{
for (int i = first; i <= last; i++) if (ar[i] > ar[first]) swap(ar[i], ar[first]);
RecursiveSort(ar,flag, (first + 1), last);
}
if (first == last) return;
}
}
It works fine if array size is 3000, but I am supposed to make this work for size>5000 and its crashing giving stack overflow.
I searched many threads and they all tell to use vectors and I have. This is an assignment problem, so I am supposed to do recursion sorting for both arrays and vectors. Its working for vectors, but not for arrays. Also I am not supposed to use pointers in this assignment.