QuickSort implementation

Hi guys please would you help me figure out why this code is crashig

it says "Unhandled exception at 0x012e1569 in QuickS.exe: 0xC00000FD: Stack overflow."
QuickSort.txt document is on its right place.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void RecursiveQuickSort (unsigned long int * a, unsigned long int n,
unsigned long int m);
void main()
{

unsigned long int A[10000];
ifstream in("QuickSort.txt");
for (unsigned long int i = 0 ; i < 10001; i++)
{
in >> A[i];

}
RecursiveQuickSort (A, 0, 10001);
}
void RecursiveQuickSort (unsigned long int * a,unsigned long int n, unsigned long int m)
{
unsigned long int i = 0, h;
if (m - n == 1)
{

}
else
{
for (unsigned long int j = n + 1; j < m; j++)
{
if (a[n] > a[j])
{
i++;
h = a[n + i];
a[n + i] = a[ j ];
a[ j ] = h;
}
}

h = a[n];
a[n] = a[n + i];
a[n + i] = h;

RecursiveQuickSort(a, n, n + i );
RecursiveQuickSort(a, n + i + 1 , m );

}

}
It doesn't look like your function will ever stop calling itself. That might be what's causing your problem.

Also, void main() should be int main().

-Albatross
I see no base case.
I didn't consider m - n = 0 case , that's why recursion never stoped, I wrote
if (m - n == 1 || m - n == 0)
{
;
}

and it worked . Thank you both for reminding me about base case and infinite calls
1
2
3
4
if (m - n == 1 || m - n == 0)
{
;
}


This actually does something?
Because it's part of an if else.
1
2
3
4
5
6
if (m - n == 1 || m - n == 0)
{
;
}
else 
  // do something 


Ooh I see why it's there now. Seems like there would be a better way to accomplish this though.

Like

1
2
if(m - n != 1 || m - n != 0)
//do something 
Topic archived. No new replies allowed.