Sindbad~EG File Manager

Current Path : /usr/home/beeson/MathXpert/polyval/
Upload File :
Current File : /usr/home/beeson/MathXpert/polyval/termsort.c

/* quicksort for additive terms */
/* M. Beeson, 3.1.98 */
/* 2.8.98 added defines for Win32 DLL compilation */
/* 12.21.99 fixed typo of "1" for "l" */
/* 5.5.13 added include termsor.h */

#include <assert.h>

#include "globals.h"
#include "order.h"
#include "termsort.h"

#define NSTACK 64
#define M 7
#define SWAP(a,b) temp = (a);(a)=(b);(b)=temp;

void termsort(int n, term *arr)
/* arr is an array of terms of dimension n */
/* sort it using addcompare */
/* Following Numerical Recipes 2nd ed., p. 333,
   but for terms instead of floats
*/

{ unsigned short i,ir=n,j,k,l=1;
  int jstack = 0;
  int *istack;
  term a,temp;
  --arr;   /* code below expects indices to begin at 1 */
  istack = callocate(NSTACK, sizeof(term));
  if(!istack)
     return;
  --istack; 
  for (;;)
     { if(ir-l < M)
          { for(j=l+1;j <= ir; j++)
               { a = arr[j];
                 for(i=j-1;i>=1;i--)
                    { if(addcompare(&arr[i],&a)<1)
                         break;
                      arr[i+1] = arr[i];
                    }
                 arr[i+1] = a;
               }
            if(jstack == 0)
               break;
            ir = istack[jstack--];
            l = istack[jstack--];
          }
       else
          { k = (l+ir) >> 1;
            SWAP(arr[k],arr[l+1])
            if(addcompare(&arr[l+1],&arr[ir])>0)
               { SWAP(arr[l+1],arr[ir])
               }
            if(addcompare(&arr[l],&arr[ir])>0)
               { SWAP(arr[l],arr[ir])
               }
            if(addcompare(&arr[l+1],&arr[l])>0)
               { SWAP(arr[l+1],arr[l])
               }
            i=l+1;
            j = ir;
            a=arr[l];
            for(;;)
               { do i++; while (i < ir && addcompare(&arr[i],&a) < 0);
                 do j--; while (j > l+1 && addcompare(&arr[j],&a) > 0);
                 /* theoretically the i < n and j > l tests are
                    superfluous and only slow things down.  But
                    if addcompare is actually not a linear order,
                    and I can't guarantee that it really is,
                    then they are NOT superfluous but will prevent
                    out-of-bounds errors.  Moreover the time cost is
                    small compared to addcompare anyway.
                 */
                 if(j < i)
                    break;
                 SWAP(arr[i],arr[j])
               }
            arr[l]=arr[j];
            arr[j]=a;
            jstack +=2;
            if(jstack > NSTACK)
               assert(0);
            if(ir-i+1 >= j-1)
               { istack[jstack]=ir;
                 istack[jstack-1]=i;
                 ir = j-1;
               }
            else
               { istack[jstack]=j-1;
                 istack[jstack-1]=l;   // typo l for 1 fixed 12.21.99 
                 l=i;
               }
          }
     }
  free2(istack+1);  // callocated at line 6 of function body
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists