# the quicksort algorithm in Numerical Recipes in C (2nd edition)

Discussion in 'C Programming' started by Edward Hua, Oct 26, 2005.

1. ### Edward HuaGuest

Hi,

I'm wondering if anybody has ever copied the quicksort algorithm from
the book Numerical Recipes in C: The Art of Scientific Computing (2nd
ed.), by Press, Teukolsky, Vetterling, and Flannery, in Chapter 8.
Quicsort is supposed to sort an array of, say, doubles, in ascending
order.

I tried to copy this algorithm line by line to test it. It seems that
there's an error in this algorithm as given in this book. Namely, the
first two entry (0th entry) of the array is never moved, and the 2nd entry
(1st entry) is also set to 0 after the sorting was supposed to be
completed. All other entries seemed to be correctly updated.

I'm wondering if anybody on this forum's ever had the same problem when
using this algorithm taken from this book. If so, could you tell me what
the problem is and how to fix it? I'm a little time-pressed to investigate

-Ed
------------------------------------------------------------------
To give an example of this error, I have an initial array of 20 doubles as
follows:

sample_array = 5.754104
sample_array = 0.973984
sample_array = 14.586500
sample_array = 11.796852
sample_array = 4.032750
sample_array = 0.618612
sample_array = 8.638782
sample_array = 2.309558
sample_array = 4.144175
sample_array = 15.159169
sample_array = 1.541796
sample_array = 18.063583
sample_array = 4.739992
sample_array = 4.905477
sample_array = 8.836610
sample_array = 15.108313
sample_array = 18.243906
sample_array = 14.667340
sample_array = 5.801667
sample_array = 2.857230

And after the sorting, it becomes:

sample_array=5.754104
sample_array=0.000000
sample_array=0.618612
sample_array=0.973984
sample_array=1.541796
sample_array=2.309558
sample_array=2.857230
sample_array=4.032750
sample_array=4.144175
sample_array=4.739992
sample_array=4.905477
sample_array=5.801667
sample_array=8.638782
sample_array=8.836610
sample_array=11.796852
sample_array=14.586500
sample_array=14.667340
sample_array=15.108313
sample_array=15.159169
sample_array=18.063583

Edward Hua, Oct 26, 2005

2. ### Ben PfaffGuest

Copying code from that book is a mistake. The explanatory text
can be insightful, but the code is not well written.

Ben Pfaff, Oct 26, 2005

3. ### Edward HuaGuest

Please ignore this question. I figured it out.

-Ed

Edward Hua, Oct 26, 2005
4. ### Rich GibbsGuest

Edward Hua said the following, on 10/26/05 10:15:
This really is not a C question as you have posed it, but I have a
suspicion that you have a C-related problem.

The original _Numerical Recipes_ book used FORTRAN, I think. I don't
have my copy of it, or of _Numerical Recipes in C_, handy, but I seem to
recall that in the C version the authors used some funky macro hacks so
that they could use 1-origin arrays (a la FORTRAN), rather than C's
0-origin arrays.

If you have an "off-by-one" problem related to this, it might account
for both of the symptoms you are seeing: the zeroth element is never
looked at; and the element 'array' (which of course doesn't exist as
far as C is concerned) _is_ used, and might be zero by chance.

Rich Gibbs, Oct 26, 2005
5. ### Edward HuaGuest

Ok. Is there a more authoritative book/on-line refrence that has an
archive of C functions (like those in NRiC)? Thanks.

-Ed

Edward Hua, Oct 26, 2005
6. ### peteGuest

#define BYTE_SWAP(A, B) \
{ \
p1 = (A); \
p2 = (B); \
end = p2 + size; \
do { \
swap = *p1; \
*p1++ = *p2; \
*p2++ = swap; \
} while (p2 != end); \
}

void q2sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *left;
size_t middle, last, right;
struct {
size_t bytes;
void *base;
} stack[CHAR_BIT * sizeof nmemb], *stack_ptr;
unsigned char *p1, *p2, *end, swap;

stack -> bytes = nmemb * size;
stack -> base = base;
stack_ptr = stack + 1;
do {
--stack_ptr;
if (stack_ptr -> bytes > size) {
left = stack_ptr -> base;
right = last = stack_ptr -> bytes - size;
middle = size;
while (compar(left, left + middle) > 0 && middle != right) {
middle += size;
}
while (compar(left + last, left) > 0) {
last -= size;
}
while (last > middle) {
BYTE_SWAP(left + middle, left + last);
do {
middle += size;
} while (compar(left, left + middle) > 0);
do {
last -= size;
} while (compar(left + last, left) > 0);
}
BYTE_SWAP(left, left + last);
if (right - last > last) {
stack_ptr -> base = left + last + size;
stack_ptr++ -> bytes = right - last;
stack_ptr -> base = left;
stack_ptr++ -> bytes = last;
} else {
stack_ptr++ -> bytes = last;
stack_ptr -> base = left + last + size;
stack_ptr++ -> bytes = right - last;
}
}
} while (stack_ptr != stack);
}

pete, Oct 27, 2005