
**SELSOR****************************************************************
* Subroutine to sort ITEM(1), ..., ITEM(N) into ascending order using  *
* the simple selection sort algorithm.  LIM is the upper limit on the  *
* size of the array ITEM.  For descending order change .LT. to .GT. in *
* the logical expression ITEM(I) .LT. SMALL.  Local variables used:    *
*      SMALL   : smallest item in current sublist                      *
*      LOCSM   : location of SMALL                                     *
*      I, J    : subscripts                                            *
*                                                                      *
* Accepts:  Array ITEM and integers LIM and N                          *
* Returns:  Array ITEM (modified) with first N elements in ascending   *
*           order                                                      *
************************************************************************

      SUBROUTINE SELSOR(ITEM, LIM, N)   

      INTEGER LIM, ITEM(LIM), N, I

      DO 20 I = 1, N - 1

*        Find smallest item in sublist ITEM(I), ..., ITEM(N)
          
         SMALL = ITEM(I)
         LOCSM = I
         DO 10 J = I + 1, N
            IF (ITEM(J) .LT. SMALL) THEN
*              Smaller item found
               SMALL = ITEM(J)
               LOCSM = J
            END IF
 10      CONTINUE

*        Interchange smallest item with ITEM(I) at
*        beginning of sublist

         ITEM(LOCSM) = ITEM(I)
         ITEM(I) = SMALL

 20   CONTINUE

      END
