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
this myself. Thanks in advance.
-Ed
------------------------------------------------------------------
To give an example of this error, I have an initial array of 20 doubles as
follows:
sample_array[0] = 5.754104
sample_array[1] = 0.973984
sample_array[2] = 14.586500
sample_array[3] = 11.796852
sample_array[4] = 4.032750
sample_array[5] = 0.618612
sample_array[6] = 8.638782
sample_array[7] = 2.309558
sample_array[8] = 4.144175
sample_array[9] = 15.159169
sample_array[10] = 1.541796
sample_array[11] = 18.063583
sample_array[12] = 4.739992
sample_array[13] = 4.905477
sample_array[14] = 8.836610
sample_array[15] = 15.108313
sample_array[16] = 18.243906
sample_array[17] = 14.667340
sample_array[18] = 5.801667
sample_array[19] = 2.857230
And after the sorting, it becomes:
sample_array[0]=5.754104
sample_array[1]=0.000000
sample_array[2]=0.618612
sample_array[3]=0.973984
sample_array[4]=1.541796
sample_array[5]=2.309558
sample_array[6]=2.857230
sample_array[7]=4.032750
sample_array[8]=4.144175
sample_array[9]=4.739992
sample_array[10]=4.905477
sample_array[11]=5.801667
sample_array[12]=8.638782
sample_array[13]=8.836610
sample_array[14]=11.796852
sample_array[15]=14.586500
sample_array[16]=14.667340
sample_array[17]=15.108313
sample_array[18]=15.159169
sample_array[19]=18.063583