      PROGRAM BUBBLESORT
      
      INTEGER LEN
      PARAMETER (LEN = 5)
      INTEGER A(LEN)
      DATA A /5, 4, 3, 2, 1/
      
      PRINT *, A
      CALL SORT(A, LEN)    
      PRINT *, A

      STOP
      END
      
      SUBROUTINE SORT(A, LEN)
      IMPLICIT NONE
      INTEGER LEN
      INTEGER A(LEN)
      INTEGER COUNT, I, TEMP
      LOGICAL SWAPPED
      
      SWAPPED = .TRUE.
      COUNT = 1
      DO WHILE ((COUNT .LE. LEN - 1) .AND. SWAPPED)
        SWAPPED = .FALSE.
        DO 20 I = 1, LEN - COUNT
          IF (A(I) .LT. A(I + 1)) THEN
            TEMP = A(I)
            A(I) = A(I + 1)
            A(I + 1) = TEMP
            SWAPPED = .TRUE.
          ENDIF
20      CONTINUE 
        PRINT *, A
        COUNT = COUNT + 1
      ENDDO

      RETURN
      END              