A sorting algorithm that merges two n×n arrays and sorts the combined result. The program creates two random arrays (a and b), merges them into a combined array (c), then sorts the merged array by repeatedly finding the maximum element and placing it in the correct position. The algorithm includes an optional step-by-step visualization mode that shows the sorting process, displaying the source array (c) and the destination array (d) at each step, with "S" marking source positions and "N" marking destination positions.
INPUT "Array propertis: ", n
INPUT "Output addtional information? [y/n] ", Out$
DIM a(n, n), b(n, n), c(n, n * 2), d(n, n * 2)
none = 0
FOR i = 1 TO n
FOR j = 1 TO n
a(i, j) = FIX(RND * n) + 1
b(i, j) = FIX(RND * n) + 1
NEXT j, i
FOR i = 1 TO n
FOR j = 1 TO n
c(i, j) = a(i, j)
c(i, j + n) = b(i, j)
NEXT j, i
FOR i = 1 TO n * n * 2
IF Out$ = "Y" OR Out$ = "y" THEN
DO WHILE INKEY$ = ""
LOOP
CLS
FOR ki = 1 TO n
FOR kj = 1 TO n * 2
IF c(ki, kj) <> none THEN PRINT c(ki, kj); ELSE PRINT " S ";
NEXT kj
PRINT
NEXT ki
PRINT
FOR ki = 1 TO n
FOR kj = 1 TO n * 2
IF d(ki, kj) <> none THEN PRINT d(ki, kj); ELSE PRINT " N ";
NEXT kj
PRINT
NEXT ki
PRINT
END IF
x = FIX((i - 1) / n / 2) + 1
y = i - n * 2 * (x - 1)
FOR j = 1 TO n * n * 2
xx = FIX((j - 1) / n / 2) + 1
yy = j - n * 2 * (xx - 1)
IF c(xx, yy) = none OR d(x, y) > c(xx, yy) THEN GOTO nexj
d(x, y) = c(xx, yy)
kx = xx
ky = yy
IF Out$ = "Y" OR Out$ = "y" THEN
PRINT "Element"; d(x, y); "in c is on ("; xx; ","; yy; ")"
END IF
nexj:
NEXT j
c(kx, ky) = none
NEXT i
FOR ki = 1 TO n
FOR kj = 1 TO n * 2
PRINT d(ki, kj);
NEXT kj
PRINT
NEXT ki
PRINT