    PROGRAM m01zafe

!      M01ZAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : m01def, m01zaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, j, ldm, m1, m2, n1, n2
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: rm(:,:)
       INTEGER, ALLOCATABLE            :: iperm(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'M01ZAF Example Program Results'

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

       READ (nin,*) m2, n2
       ldm = m2
       ALLOCATE (rm(ldm,n2),iperm(m2))

       m1 = 1
       n1 = 1

       DO i = m1, m2
          READ (nin,*) (rm(i,j),j=n1,n2)
       END DO

       ifail = 0
       CALL m01def(rm,ldm,m1,m2,n1,n2,'Ascending',iperm,ifail)

       ifail = 0
       CALL m01zaf(iperm,m1,m2,ifail)

       WRITE (nout,*)
       WRITE (nout,*) 'Matrix sorted by rows'
       WRITE (nout,*)

       DO i = m1, m2
          WRITE (nout,99999) (rm(iperm(i),j),j=n1,n2)
       END DO

99999  FORMAT (1X,3F7.1)
    END PROGRAM m01zafe
