EXTERNAL

! =======================================================================
!
! LIBRARY: Sort.lib
!
! This library provides access to several sort routines implemented
! in C and linked to True BASIC as libraries.
!
! SUBROUTINES: Sort()
!              Bubble_Sort()
!              Comb_Sort()
!
! Paul Castonguay                                       December 11, 1991
! =======================================================================




! =======================================================================
!
! SUBROUTINE: Sort()
!
! PURPOSE: To sort an array of numerical data passed by the calling
!          program using any one of several available sorting routines.
!
!
! PARAMETERS:
!
!    INPUT:
!
!
!       Low ................... Lower limit of section of array that
!                               you want sorted.
!
!       High .................. Upper limit of section of array that
!                               you want sorted.
!
!       Data_Type$ ............ allowed arguments : INT
!                                                   FLOAT
!
!                               Identifies data as integer (INT) or 
!                               floating point (FLOAT).  If you specify
!                               INT and pass floating point data it will
!                               get converted to integer and then it
!                               will be sorted.
!
!       Sort_Method$ .......... allowed arguments: BUBBLE
!                                                  COMB
!                                               
!
!    IN/OUT
!
!       Numeric_Data() ........ array containing data to be sorted.
!
!
!    OUTPUT: NONE
!
!
! PRECONDITIONS: Data to be sorted is stored in numeric array in the 
!                calling program and passed to this routine by reference.
!
! POSTCONDITIONS: Data in passed array is sorted.
!
!
! Paul Castonguay                                       December 10, 1991
! =======================================================================                         
SUB Sort(Numeric_Data(), Low, High, Data_Type$, Sort_Method$)


   ! ****************************
   ! Trap impossible array limits
   ! ****************************
   IF High-Low <= 0 THEN
      WINDOW #0
      CLEAR
      PRINT "Error in Sort()"
      PRINT "Incorrect array range to sort."
      STOP
   END IF


   ! *****************************
   ! Test for desired sort routine
   ! *****************************
   SELECT CASE UCASE$(Sort_Method$)

      CASE "BUBBLE"

         CALL Bubble_Sort(Numeric_Data, Low, High, Data_Type$)

      CASE "COMB"

         CALL Comb_Sort(Numeric_Data, Low, High, Data_Type$)

      CASE ELSE

         WINDOW #0
         CLEAR
         PRINT "Error in Sort()"
         PRINT "Sort algorithm specified incorrectly."
         STOP

   END SELECT


END SUB  ! End of Sort()




! =======================================================================
!
! SUBROUTINE: Bubble_Sort()
!
! PURPOSE: To sort an array of numerical data passed by the calling
!          program using the classic, but slow, bubble sort algorithm.
!
!
! PARAMETERS:
!
!    INPUT:
!
!
!       Low ................... Lower limit of section of array that
!                               you want sorted.
!
!       High .................. Upper limit of section of array that
!                               you want sorted.
!
!       Data_Type$ ............ allowed arguments : INT
!                                                   FLOAT
!
!                               Identifies data as integer (INT) or 
!                               floating point (FLOAT).  If you specify
!                               INT and pass floating point data it will
!                               get converted to integer and then it 
!                               will be sorted.
!
!    IN/OUT
!
!       Numeric_Data() ........ array containing data to be sorted.
!
!
!    OUTPUT: NONE
!
!
! PRECONDITIONS: Data to be sorted is stored in numeric array in the 
!                calling program and passed to this routine by reference.
!
! POSTCONDITIONS: Data in passed array is sorted.
!
!
! Paul Castonguay                                       December 11, 1991
! =======================================================================                         
SUB Bubble_Sort(Numeric_Data(), Low, High, Data_Type$)

   LET Number_of_Elements = High-Low+1

   ! *************************************************
   ! Test data type.  Different sort routines are used
   ! for integer and floating point types of data.
   ! *************************************************
   SELECT CASE UCASE$(Data_Type$)

      CASE "INT"
   
         ! ************************************
         ! Pack numeric data into a long string
         ! ************************************
         LET String_Data$ = ""
         FOR I = 1 TO Number_of_Elements
            CALL Packb(Data_Element$, 1, 32, Numeric_Data(Low+I-1))
            LET String_Data$ = String_Data$ & Data_Element$
         NEXT I
   
         CALL IntBubble(String_Data$)
   
         ! **************************************
         ! UpPack numeric data into a long string
         ! **************************************
         FOR I = 1 TO Number_of_Elements
            LET Numeric_Data(Low+I-1) = Unpackb(String_Data$, (I-1)*32+1, -32)
         NEXT I

      CASE "FLOAT"

         ! *************************************************
         ! Convert numeric data to IEEE double precision
         ! format and concatenate into one contiguous string
         ! *************************************************
         LET String_Data$ = ""
         FOR I = 1 TO Number_of_Elements
            LET Data_Element$ = NUM$(Numeric_Data(Low+I-1))
            LET String_Data$ = String_Data$ & Data_Element$
         NEXT I
   
         CALL FloatBubble(String_Data$)
   
         ! *******************************************
         ! Convert sorted data to True BASIC numeric
         ! format and replace values in original array
         ! *******************************************
         FOR I = 1 TO Number_of_Elements
            LET Numeric_Data(Low+I-1) = NUM(String_Data$[(I-1)*8+1:(I-1)*8+8])
         NEXT I
   
      CASE ELSE

         WINDOW #0
         CLEAR
         PRINT "Error in Sort()"
         PRINT "Data type specified incorrectly."
         STOP

   END SELECT


END SUB  ! End of Bubble_Sort()



! =======================================================================
!
! SUBROUTINE: Comb_Sort()
!
! PURPOSE: To sort an array of numerical data passed by the calling
!          program using the Combsort by Stephen Lacey and Richard Box,
!          first published in BYTE Magazine, April 1991.
!
!
! PARAMETERS:
!
!    INPUT:
!
!
!       Low ................... Lower limit of section of array that
!                               you want sorted.
!
!       High .................. Upper limit of section of array that
!                               you want sorted.
!
!       Data_Type$ ............ allowed arguments : INT
!                                                   FLOAT
!
!                               Identifies data as integer (INT) or 
!                               floating point (FLOAT).  If you specify
!                               INT and pass floating point data it will
!                               get converted to integer and then it 
!                               will be sorted.
!
!    IN/OUT
!
!       Numeric_Data() ........ array containing data to be sorted.
!
!
!    OUTPUT: NONE
!
!
! PRECONDITIONS: Data to be sorted is stored in numeric array in the 
!                calling program and passed to this routine by reference.
!
! POSTCONDITIONS: Data in passed array is sorted.
!
!
! Paul Castonguay                                       December 11, 1991
! =======================================================================                         
SUB Comb_Sort(Numeric_Data(), Low, High, Data_Type$)

   LET Number_of_Elements = High-Low+1

   ! *************************************************
   ! Test data type.  Different sort routines are used
   ! for integer and floating point types of data.
   ! *************************************************
   SELECT CASE UCASE$(Data_Type$)

      CASE "INT"
   
         ! ************************************
         ! Pack numeric data into a long string
         ! ************************************
         LET String_Data$ = ""
         FOR I = 1 TO Number_of_Elements
            CALL Packb(Data_Element$, 1, 32, Numeric_Data(Low+I-1))
            LET String_Data$ = String_Data$ & Data_Element$
         NEXT I
   
         CALL IntComb(String_Data$)
   
         ! **************************************
         ! UpPack numeric data into a long string
         ! **************************************
         FOR I = 1 TO Number_of_Elements
            LET Numeric_Data(Low+I-1) = Unpackb(String_Data$, (I-1)*32+1, -32)
         NEXT I

      CASE "FLOAT"

         ! *************************************************
         ! Convert numeric data to IEEE double precision
         ! format and concatenate into one contiguous string
         ! *************************************************
         LET String_Data$ = ""
         FOR I = 1 TO Number_of_Elements
            LET Data_Element$ = NUM$(Numeric_Data(Low+I-1))
            LET String_Data$ = String_Data$ & Data_Element$
         NEXT I
   
         CALL FloatComb(String_Data$)
   
         ! *******************************************
         ! Convert sorted data to True BASIC numeric
         ! format and replace values in original array
         ! *******************************************
         FOR I = 1 TO Number_of_Elements
            LET Numeric_Data(Low+I-1) = NUM(String_Data$[(I-1)*8+1:(I-1)*8+8])
         NEXT I
   
      CASE ELSE

         WINDOW #0
         CLEAR
         PRINT "Error in Sort()"
         PRINT "Data type specified incorrectly."
         STOP

   END SELECT


END SUB  ! End of Comb_Sort()

