100 REM Merge Sort
130 DECLARE EXTERNAL SUB MergeSort.sort
140 OPTION ARITHMETIC NATIVE
150 DIM m(100000)               !
160 LET n=10000                 ! actual number of data
170 MAT m=ZER(n)                !
180 FOR i=1 TO n                ! test data
190    LET m(i)=INT(RND*10000)
200 NEXT i
210 CALL sort(m,LBOUND(m),UBOUND(m))
220 MAT PRINT m;
230 END
240 
250 MODULE MergeSort
260 MODULE OPTION ARITHMETIC NATIVE
270 SHARE NUMERIC t(100000)       ! 
280 EXTERNAL SUB sort(m(),a,b)
290    IF a<b THEN
300       LET c=INT((a+b)/2)
310       CALL sort(m,a,c)        ! 
320       CALL sort(m,c+1,b)      ! 
330       FOR i=a TO b            ! 
340          LET t(i)=m(i)
350       NEXT i
360       LET i=a                 ! 
370       LET j=c+1
380       FOR k=a TO b
390          IF j>b OR i<=c AND t(i)<t(j) THEN
400             LET m(k)=t(i)
410             LET i=i+1
420          ELSE
430             LET m(k)=t(j)
440             LET j=j+1
450          END IF
460       NEXT k
470    END IF 
480 END SUB
490 END MODULE
 