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

From: Carl Barron (cbarron413_at_adelphia.net)
Date: 12/31/04


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....



Relevant Pages

  • Re: The Advantage of Macros
    ... which is usually a single source file, can consist of external declarations ... void fx(int *x, int *y) { ... Also, if you think C is complicated, just take a look at languages like C++, ... C pushes to wrong programming styles. ...
    (alt.lang.asm)
  • Re: Bug/Gross InEfficiency in HeathFields fgetline program
    ... Maybe this is true in the sort of programming you do, ... you are storing a list of amounts of money as integers. ... int average ...
    (comp.lang.c)
  • C Programs
    ... C is mother of all modern computer programming languages. ... There are many basic to advanced C source codes given on this website, ... Sorting methods Algorithams impemented in C such as bubble sort, ...
    (comp.lang.c)
  • Re: robot at cheap price...
    ... What sort of things do you want to do with your robot? ... What sort of programming do you: what languages, ...
    (comp.robotics.misc)
  • Re: Unicode
    ... > int main ... if you mean making your code translatable to different programming ... languages, you could just substitute cout with output ...
    (comp.lang.cpp)