/* ============================ IntBubble.c =============================

   C subroutine to sort an array of integer data using the classic
   and very slow bubble sort.


   ================           MECHANISM BEHIND             ==============
   ================ PASSING ARGUMENTS FROM TRUE BASIC TO C ==============
   
Arguments are passed from True BASIC to C through **arg.

Arg is a pointer to a list of pointers.  In this case we have only one 
argument, the unsorted data.  It is passed as a pointer to a TBString
The data is in the text member of that TBString structure.  It is your
responsibility to ensure that the data is loaded into that string
in the proper form on the True BASIC side before calling this routine.


      ________         ___________          __________________
     |        |       |           |        |                  |
     | **arg -------->| *TBString |------->| struct TBString  |
     |________|       |___________|        |__________________|
                                           |        |         |
                                           | length | text[0] |
                                           |________|_________|


TBString members:  long length ........ length of string passed.
                   text[0] ............ actual string, not NULL 
                                        terminated.
     
                                        
   ========================   MECHANISM BEHIND  =========================
   ======================== COMPILING THIS FILE =========================
                                        
C code is compiled and linked by SAS/C but not to the usual startup code
LIB:c.o.  To do so would produce a self standing executable program and
we don't want that.  We are producing a compiled routine that will be 
invoked by True BASIC.  Here is the lmkfile for this routine:

                 PROGRAM = IntBubble

                 $(PROGRAM) : $(PROGRAM).o
                    BLink FROM $(PROGRAM).o TO $(PROGRAM)*

                 $(PROGRAM).o : $(PROGRAM).c
                    LC -b0 -cfistc -v -O $(PROGRAM)

   ======================================================================


SUBROUTINE: IntBubble()

INPUT:

   *data_string ..... TBString structure containing array of unsorted data.

                    
PRECONDITIONS:
  
   Integer values must be in 2's complement form and joined together as 
   a contiguous length of long integers.  You put the data in that form 
   on the True BASIC side by packing it into a long character string, 
   using the Packb() subroutine.  That is, you must copy the numeric data 
   from its numeric array into a contiguous character string, where each
   each integer occupies exactly 4 bytes.  Pass the string to this 
   routine.

    
POSTCONDITIONS: 

   Data in original memory space is sorted.  Note that it is still in
   packed form.  You must now unpack it, using True BASIC's Unpackb() 
   function.
   
      
Paul Castonguay                                      December 10, 1991
====================================================================== */
#include <exec/types.h>
#include <TB.h>

/* ============================================ */
/* === ANSI function prototype declarations === */
/* ============================================ */
VOID CMultiply(LONG **arg, struct TBUserVector *uv);


VOID IntBubble(long **arg, struct TBUserVector *uv)
{
   /* =========================================== */
   /* === Pointer to hold address of argument === */
   /* ===          structures passed.         === */
   /* =========================================== */
   struct TBString *string;

   LONG *beginning;       /* points to beginning of integer data  */
   LONG *end;             /* points to end of integer data        */
   LONG *outer;           /* outside loop pointer for bubble sort */
   LONG *inner;           /* inner loop pointer for bubble sort   */
   LONG array_length;     /* number of integers to be sorted      */
   LONG temp;             /* used for swaping element positions   */


   /* ======================================================== */
   /* === Pick up address of string passed from True BASIC === */
   /* ======================================================== */
   string = (struct TBString *) **arg;


   /* =================================================== */
   /* === Measure length of data, number of integers. === */
   /* =================================================== */
   array_length = string->length/4;
   
   
   /* =========================== */
   /* === Initialize pointers === */
   /* =========================== */
   beginning = (long *)&(string->text[0]);
   end = beginning + array_length - 1;


   /* ============================= */
   /* === Bubble sort algorithm === */
   /* ============================= */
   for(outer = beginning; outer < end; outer++)
      for(inner = beginning; inner < end-(outer-beginning); inner++)
      {
         if(*inner > *(inner+1))
         {
            temp = *inner;
            *inner = *(inner+1);
            *(inner+1) = temp;
         }
      }
}
