www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/12/04:02:57

From: "A.Appleyard" <A DOT APPLEYARD AT fs2 DOT mt DOT umist DOT ac DOT uk>
Organization: Materials Science Centre
To: DJGPP AT DELORIE DOT COM
Date: Wed, 12 Jun 1996 08:55:46 GMT
Subject: Useful source form fragments?
Message-ID: <2B4FE460DCF@fs2.mt.umist.ac.uk>

  (1) Compact bubble sort:-
  typedef struct {double x; rhubarb; rhubarb; rhubarb; rhubarb;} miaow;
  miaow X[256];

/* sort miaow[B] to miaow[N] inclusive into ascending order of element x */
miaow *I,*J,W;
for(k=1,I=X+N;k;I--) for(k=0,J=X+B;J<I;J++) if(J[0].x>J[1].x) {
    k=1; W=J[0]; J[0]=J[1]; J[1]=W; }
/* the outer loop stops when the inner loop finds no more sorting to do */

  (1) Compact permutation:-
int i,j,x[N],b[N],e[N],n=N; for(i=0;i<n;i++) {b[i]=x[i]=0; e[i]=3; }

for(x[(i=n)-1]--; i>0; x[i]=b[i]) for(i--; (++x[i])<=e[i]; i=n-1) { /* XX */
    for(j=0;j<n;j++) putchar('0'+x[j]); putchar('\n'); }

  and the line XX does the same as:-
  for(x[n-1]=b[n-1];x[n-1]<=e[n-1];x[n-1]++)
    for(x[n-2]=b[n-2];x[n-2]<=e[n-2];x[n-2]++) .... etc to
          for(x[2]=b[2];x[2]<=e[2];x[2]++)
            for(x[1]=b[1];x[1]<=e[1];x[1]++)
              for(x[0]=b[0];x[0]<=e[0];x[0]++) {

  If `x[(i=n)-1]--' is replaced by `i=n-1', it omits the first iteration,
where all the x's = all the b's; I find that often this first iteration has to
be run separately to set things up.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019