Re: Need help for a code in "Numerical Recipes in C++"

From: George Bush (george.w.bush_at_whitehouse.com)
Date: 01/02/05


Date: Sun, 02 Jan 2005 04:02:11 GMT

Why not look at the code in

http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html

and skip typing in the NR stuff.

In article <cr3osr$tn7$02$1@news.t-online.com>, Mok-Kong Shen
<mok-kong.shen@t-online.de> wrote:
>
>The code of the heapsort algorithm as given in books by
>e.g. Sedgewick deals with arrays with indices in [1..n].
>For programming in languages like C, one would like to sort
>arrays with indices in [0..n-1]. I have found sofar only one
>book that gives code for the latter case, namely "Numerical
>Recipes in C++" by W. H. Press et al., which itself refers
>to Sedgewick. (Note that a companion book by the same authors
>for C has code for indices in [1..n], which is a bit odd in
>that context.)
>
>I adapted the code in the C++ book to C for sorting arrays
>of 'int' with indices in [0..n-1]. However, the resulting
>code (see attachment below) does not always function correctly.
>As concrete examples, of 8 randomly chosen cases tested for an
>'int' array of size 5 consisting of [0..4] the following input
>
> 2 1 3 0 4
>
>was wrongly sorted to
>
> 0 1 2 4 3
>
>while the following inputs
>
> 3 0 4 2 1
> 4 1 0 2 3
> 4 3 0 1 2
> 3 0 1 4 2
> 2 3 1 4 0
> 2 3 4 0 1
> 1 0 4 3 2
>
>were all processed correctly.
>
>Since I have only the book but not the corresponding original
>software, I couldn't be sure whether this is due to my coding
>error in adapting the code from the text of the book or whether
>this is due to an error of the authors of the book (though I
>have carefully checked my coding several times to be quite sure
>that it corresponds in essence to the code given in the book).
>If there is some person who happens to possess the original
>software of the C++ book, I should appreciate very much to know
>a confirmation/refutation of my computing result above.
>
>M. K. Shen
>----------------------------------------------
>
>void siftdown(int a[], int l, int r)
>{ int j,jold,v;
> v=a[l];
> jold=l;
> j=l+1;
> while (j<=r)
> { if (j<r && a[j]<a[j+1]) j++;
> if (v>=a[j]) break;
> a[jold]=a[j];
> jold=j;
> j=2*j+1;
> }
> a[jold]=v;
>}
>
>void hpsort(int a[], int n)
>{ int i,t;
> for (i=n/2-1; i>=0; i--) siftdown(a,i,n-1);
> for (i=n-1; i>0; i--)
> { t=a[0]; a[0]=a[i]; a[i]=t;
> siftdown(a,0,i-1);
> }
>}
>



Relevant Pages

  • Re: Why no type hints for built-in types?
    ... Maybe an alternative to "int" and "float" hints ... enforce that using "int" as type hint. ... general, and there may also be good reasons for not having it, but given ... typing (to a varying degree, the newer ones being more strict than the older ...
    (alt.php)
  • Re: Why no type hints for built-in types?
    ... Maybe an alternative to "int" and "float" hints ... enforce that using "int" as type hint. ... general, and there may also be good reasons for not having it, but given ... typing (to a varying degree, the newer ones being more strict than the older ...
    (comp.lang.php)
  • Re: dynamic vs. static: the age-old debate
    ... int addx+y; ... orthogonal to static/dynamic/soft typing. ... $ dynamic-compiler program.source -o program ... will overflow, leaving the "overflow behavior is undefined" ...
    (comp.lang.misc)
  • Re: dynamic vs. static: the age-old debate
    ... int addx+y; ... orthogonal to static/dynamic/soft typing. ... $ dynamic-compiler program.source -o program ... will overflow, leaving the "overflow behavior is ...
    (comp.lang.misc)
  • Re: Help for Simplescalars cache.c code
    ... I'm trying to understand this fragment of code in Simplescalar's ... cp is an structure for a cache ... int set_shift; ... about caches and this type of coding can help out. ...
    (comp.programming)