      INTEGER, INTENT(IN) :: n,k
      INTEGER   I,J,L,R,TEST
      PARAMETER (TEST=1)
!***********************************************************************
      IF ((K.LT.1).OR.(K.GT.N))RETURN
      L=1
      R=N
      DO
	 IF (L.GE.R)EXIT
	 X=A(K)
	 I=L
	 J=R
20       IF (A(I).GE.X)GO TO 30
	 I=I+1
	 GO TO 20
30       IF (A(J).LE.X)GO TO 40
	 J=J-1
	 GO TO 30
40       IF (I.GT.J)GO TO 50
	 W=A(I)
	 A(I)=A(J)
	 A(J)=W
	 I=I+1
	 J=J-1
	 IF (I.LE.J)GO TO 20
50       IF (J.LT.K)L=I
	 IF (K.LT.I)R=J
      ENDDO
!***** VERIFY
      IF (TEST.NE.1)RETURN
      DO L=1,K
	 IF (A(L).GT.A(K))STOP ' ASSERT VIOLATION IN "MEDIAN"'
      ENDDO
      DO L=K,N
	 IF (A(L).LT.A(K))STOP ' ASSERT VIOLATION IN "MEDIAN"'
      ENDDO
      RETURN
