    PROGRAM m01ebfe

!      M01EBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : m01dbf, m01ebf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, j, k, m1, m2, n
!      .. Local Arrays ..
       INTEGER, ALLOCATABLE            :: im(:,:), irank(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'M01EBF Example Program Results'

!      Skip heading in data file
       READ (nin,*)

       READ (nin,*) m2, n, k

       IF (k<1 .OR. k>n) THEN
          GO TO 20
       END IF

       ALLOCATE (im(m2,n),irank(m2))

       m1 = 1

       DO i = m1, m2
          READ (nin,*) (im(i,j),j=1,n)
       END DO

       ifail = 0
       CALL m01dbf(im(m1,k),m1,m2,'Ascending',irank,ifail)

       DO j = 1, n

          ifail = 0
          CALL m01ebf(im(m1,j),m1,m2,irank,ifail)

       END DO

       WRITE (nout,*)
       WRITE (nout,99999) 'Matrix sorted on column', k
       WRITE (nout,*)

       DO i = m1, m2
          WRITE (nout,99998) (im(i,j),j=1,n)
       END DO

20     CONTINUE

99999  FORMAT (1X,A,I3)
99998  FORMAT (1X,3I7)
    END PROGRAM m01ebfe
