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

From: Mok-Kong Shen (mok-kong.shen_at_t-online.de)
Date: 12/31/04


Date: Fri, 31 Dec 2004 21:16:45 +0100


Carl Barron wrote:

> 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.)
>> [Snip]
>>void siftdown(int a[], int l, int r)
>>{ int j,jold,v;
>> v=a[l];
>> jold=l;
>> j=l+1;
>
> above should be j = 2*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);
>> }
>>}
>>
>
> Now it looks like a a translation of hpsort() to C for int[]. The C++
> code works There code works....

Do you happen to have an explanation of the trouble? Thanks.

M. K. Shen



Relevant Pages

  • If this woman becomes Vice-President, I really have to wonder if we wont look back at this perio
    ... of this truly remarkable tree, with roots that have grown so ... creative sort of guy, I subsequently added all sorts of things, ... Of course Sarah Palin must think the torch held by the famous gift ... of books Palin wanted banned from the Wasilla Library. ...
    (misc.news.internet.discuss)
  • Re: Splitting the genres
    ... but rather "Find an easily implemented, totally useless sort". ... bring new acquisitions home but tend not to have all my books *at* ... I've also stored audio tapes in a slightly ... purchase cassette tapes.) ...
    (rec.arts.sf.written)
  • Re: Racists In This Group
    ... >> experience with the reader. ... That is one reason that I often prefer books ... > I think of trouble of that sort. ... I couldn't find that title today, but found another fowles; ...
    (alt.usage.english)
  • Re: Editors: threat or menace?
    ... of some sort) for them. ... I suspect the ability to track transactions will ... People buy books because they like to buy books, ... Welcome back to the Wild Wild Web: ...
    (rec.arts.sf.written)
  • Re: New Riding Buddy (heheh)
    ... checking out 'The Joy of Gay Sex' on her next trip to the library. ... tube) is much more egregious than any sort of book available in the ... let's please focus on those evil sex books that corrupt young ... But that sound is quite rare. ...
    (alt.mountain-bike)

Quantcast