REM Сравнение методов сортировки
DECLARE SUB BUBBLE (X%(), N%)
DECLARE SUB INSERT (X%(), N%)
DECLARE SUB SELECT1 (X%(), N%)
DECLARE SUB SHELLSORT (X%(), N%)
DECLARE SUB HOARE (X%(), N%)
DECLARE SUB QUICK (X%(), LEFT%, RIGHT%)
DEFINT A-Z
CLS
CONST N = 5000
DIM A(N)
FOR J = 0 TO N
A(J) = INT(N * RND)
' PRINT USING "#### "; A(J);
NEXT J
PRINT
T1# = TIMER
'BUBBLE A(), N
'INSERT A(),N
'SELECT1 A(),N
'SHELLSORT A(),N
HOARE A(), N
T2# = TIMER
PRINT T2# - T1#; "сек"
'FOR J = 0 TO N: PRINT USING "#### "; A(J); : NEXT J
END
SUB BUBBLE (X%(), N%)
DEFINT A-Z
FOR I = 1 TO N - 1
FOR J = N - 1 TO I STEP -1
IF X(J - 1) > X(J) THEN
TMP = X(J - 1): X(J - 1) = X(J): X(J) = TMP
END IF
NEXT J
NEXT I
END SUB
SUB HOARE (X%(), N%)
DEFINT A-Z
QUICK X%(), 0, N% - 1
END SUB
SUB INSERT (X%(), N%)
DEFINT A-Z
DIM A, B, TMP
FOR A = 1 TO N - 1
TMP = X(A)
FOR B = A - 1 TO 0 STEP -1
IF TMP > X(B) THEN EXIT FOR
X(B + 1) = X(B)
NEXT B
X(B + 1) = TMP
NEXT A
END SUB
SUB QUICK (X%(), LEFT%, RIGHT%)
DEFINT A-Z
DIM I, J, XX, YY
I = LEFT%: J = RIGHT%
XX = X((LEFT% + RIGHT%) \ 2)
DO
WHILE X%(I) < XX AND I < RIGHT%: I = I + 1: WEND
WHILE XX < X%(J) AND J > LEFT%: J = J - 1: WEND
IF I <= J THEN
YY = X%(I): X%(I) = X%(J): X%(J) = YY: I = I + 1: J = J - 1
END IF
LOOP WHILE I <= J
IF LEFT% < J THEN QUICK X%(), LEFT%, J
IF I < RIGHT% THEN QUICK X%(), I, RIGHT%
END SUB
SUB SELECT1 (X%(), N%)
DEFINT A-Z
FOR I = 0 TO N - 2
Q = 0: K = I: TMP = X(I)
FOR J = I + 1 TO N - 1
IF X(J) < TMP THEN K = J: TMP = X(J): Q = 1
NEXT J
IF Q = 1 THEN X(K) = X(I): X(I) = TMP
NEXT I
END SUB
SUB SHELLSORT (X%(), N%)
DEFINT A-Z
DIM I, J, GAP, K, XX, A(5)
A(0) = 9: A(1) = 5: A(2) = 3: A(3) = 2: A(4) = 1
FOR K = 0 TO 4
GAP = A(K)
FOR I = GAP TO N - 1
XX = X(I)
FOR J = I - GAP TO 0 STEP -GAP
IF XX >= X(J) THEN EXIT FOR
X(J + GAP) = X(J)
NEXT J
X(J + GAP) = XX
NEXT I
NEXT K
END SUB