problem with sort (quicksort)


ellendb
07-08-2004, 04:01 AM
Dear list,

i try to sort a vector using the sort(Vec_IO_DP &arr) function (in Visual C++) as described in the book on page 337, section 8.2. It works fine for vectors with size smaller than 7. I understand that the M constant as declared in the sort algorithm is set to 7 for some memory/execution purpose.

If I use an array with size 7 or more, I get the error below in the else part of the if (ir-1<M) part, more exactly when I execute the rule istack[jstack] = ir; just after the test if (ir-i+1 <= j-1).

ERROR MESSAGE: Unhandled exception, access violation.

I need to sort a vector of which I do not know the size in advance. How can I do this?

Thanx,

Ellen

Clarence
07-14-2004, 09:37 AM
Dear Ellen

First I assume that the line of code

(ir-i+1 <= j-1)

is a typo for

(ir-i+1 >= j-1).

Second I suggest that you get a copy of the Numerical Recipes Example Book
and the CDROM. The driver for sort is xsort and the example data is tarray.dat.
Here the size of the array is hard coded as 100, but this could be changed
to either read in a number from the file (add another line to the file) or
you could count the number of elements. When the array is passed to sort,
the size is found there by

int n=arr.size();

Is this useful?? I hope so.

Clarence