Re: Need help for a code in "Numerical Recipes in C++"
From: Carl Barron (cbarron413_at_adelphia.net)
Date: 12/31/04
- Next message: Mok-Kong Shen: "Re: Need help for a code in "Numerical Recipes in C++""
- Previous message: carlos_at_colorado.edu: "Re: Eigenvalues and eigenvectors of an ill-conditioned matrix"
- In reply to: Mok-Kong Shen: "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 14:41:07 -0500
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.)
> [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....
- Next message: Mok-Kong Shen: "Re: Need help for a code in "Numerical Recipes in C++""
- Previous message: carlos_at_colorado.edu: "Re: Eigenvalues and eigenvectors of an ill-conditioned matrix"
- In reply to: Mok-Kong Shen: "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
|