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
- Next message: Mok-Kong Shen: "Re: Need help for a code in "Numerical Recipes in C++""
- Previous message: Carl Barron: "Re: Need help for a code in "Numerical Recipes in C++""
- In reply to: Carl Barron: "Re: Need help for a code in "Numerical Recipes in C++""
- Next in thread: Mok-Kong Shen: "Re: Need help for a code in "Numerical Recipes in C++""
- Reply: Mok-Kong Shen: "Re: Need help for a code in "Numerical Recipes in C++""
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Mok-Kong Shen: "Re: Need help for a code in "Numerical Recipes in C++""
- Previous message: Carl Barron: "Re: Need help for a code in "Numerical Recipes in C++""
- In reply to: Carl Barron: "Re: Need help for a code in "Numerical Recipes in C++""
- Next in thread: Mok-Kong Shen: "Re: Need help for a code in "Numerical Recipes in C++""
- Reply: Mok-Kong Shen: "Re: Need help for a code in "Numerical Recipes in C++""
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|