Sorting algorithms/Merge sort: Difference between revisions
Content deleted Content added
m →{{header|Tcl}}: minor formatting |
PL/1 version |
||
Line 692:
let _ =
merge_sort compare [8;6;4;2;1;3;5;7;9]</lang>
=={{header|PL/1}}==
<lang PL/1>
MERGE: PROCEDURE (A,LA,B,LB,C);
/* Merge A(1:LA) with B(1:LB), putting the result in C
B and C may share the same memory, but not with A.
*/
DECLARE (A(*),B(*),C(*)) BYADDR POINTER;
DECLARE (LA,LB) BYVALUE NONASGN FIXED BIN(31);
DECLARE (I,J,K) FIXED BIN(31);
DECLARE (SX) CHAR(58) VAR BASED (PX);
DECLARE (SY) CHAR(58) VAR BASED (PY);
DECLARE (PX,PY) POINTER;
I=1; J=1; K=1;
DO WHILE ((I <= LA) & (J <= LB));
PX=A(I); PY=B(J);
IF(SX <= SY) THEN
DO; C(K)=A(I); K=K+1; I=I+1; END;
ELSE
DO; C(K)=B(J); K=K+1; J=J+1; END;
END;
DO WHILE (I <= LA);
C(K)=A(I); I=I+1; K=K+1;
END;
DO WHILE (J <= LB);
C(K)=B(J); J=J+1; K=K+1;
END;
RETURN;
END MERGE;
MERGESORT: PROCEDURE (AP,N) RECURSIVE ;
/* Sort the array AP containing N pointers to strings */
DECLARE (AP(*)) BYADDR POINTER;
DECLARE (N) BYVALUE NONASGN FIXED BINARY(31);
DECLARE (M,I) FIXED BINARY;
DECLARE AMP1(1) POINTER BASED(PAM);
DECLARE (pX,pY,PAM) POINTER;
DECLARE SX CHAR(58) VAR BASED(pX);
DECLARE SY CHAR(58) VAR BASED(pY);
IF (N=1) THEN RETURN;
M = trunc((N+1)/2);
IF (M>1) THEN CALL MERGESORT(AP,M);
PAM=ADDR(AP(M+1));
IF (N-M > 1) THEN CALL MERGESORT(AMP1,N-M);
pX=AP(M); pY=AP(M+1);
IF SX <= SY then return; /* Skip Merge */
DO I=1 to M; TP(I)=AP(I); END;
CALL MERGE(TP,M,AMP1,N-M,AP);
RETURN;
END MERGESORT;
</lang>
=={{header|Prolog}}==
|