
C** SORTIT ** SUBROUTINE TO SORT RAD50 ARRAYS INTO ORDER
C
C
C---- INPUT IS A WORD ARRAY
C--ARRAY 'IN' CONTAINS THE DATA TO BE SORTED. 'IORD' IS RETURNED W
C---THE SUBSCRIPS OF 'IN' SORTED INTO ORDER. 
C---- ARRAYS 'ILB' AND 'IRB' ARE WORKING ARRAYS. ARRAY 'IN'
C-----CONTAINS 'M' RECORDS AND 'N' DATA  IN EACH RECORD.
C---UPON COMPLETION, 'IORD' CONTAINS THE (SECOND) SUBSCRIPTS
C-----OF 'IN' IN INCREASING ORDER.
C---THIS IS A "MONKEY PUZZLE TREE" SORT AND IS VERY FAST.
C	SINCE RAD50 VALUES CAN EXCEED 32767 AND BECOME NEGATIVE,
C	WE MUST COMPENSATE FOR OVERFLOW BEFORE SORTING.
C
	SUBROUTINE SORTIT (IN, N, M, IORD, IRB, ILB, IORDER, N1)
C
	DIMENSION ILB(M), IRB(M), IN(N,M), IORD(M), IORDER(N1)
C
D	CALL OUTMSG ('START SORT')
D	CALL ASSIGN (2,'KB:DATA.DAT',0)
D	WRITE (2,99) ((IN(I,J),I=1,8),J=1,M)
D99	FORMAT (8O7)
	ILB(1)=0
	IRB(1)=0
	DO 18 I=2,M
	ILB(I)=0
	IRB(I)=0
2	J=1
4	DO 6 L=1,N1
	LL=IORDER(L)
	IF (IN(LL,I) .NE. IN(LL,J)) GO TO 8
6	CONTINUE
8	CONTINUE
	IF (IN(LL,I) .GT. 0) I1=IN(LL,I)-32766
	IF (IN(LL,J) .GT. 0) I2=IN(LL,J)-32766
	IF (IN(LL,I) .LT. 0) I1=IN(LL,I).AND."77777
	IF (IN(LL,J) .LT. 0) I2=IN(LL,J).AND."77777
	IF (I1 .GT. I2) GO TO 14
C??	IF (IN(LL,I) .GT. IN(LL,J)) GO TO 14
10	IF (ILB(J) .EQ. 0) GO TO 12
	J=ILB(J)
	GO TO 4
12	IRB(I)=-J
	ILB(J)=I
	GO TO 18
14	IF (IRB(J) .LE. 0) GO TO 16
	J=IRB(J)
	GO TO 4
16	IRB(I)=IRB(J)
	IRB(J)=I
18	CONTINUE
C--NOW STRIP THE MONKY PUZZLE TREE
	L=1
	J=1
	GO TO 22
20	J=ILB(J)
22	IF (ILB(J) .GT. 0) GO TO 20
24	IORD(L)=J
	L=L+1
26	IF (IRB(J)) 30, 32, 28
28	J=IRB(J)
	GO TO 22
30	J=-IRB(J)
	GO TO 24
32	RETURN
	END


