home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Shareware - Software Farm 2
/
wosw_2.zip
/
wosw_2
/
QBAS
/
BAS_SORT.ZIP
/
CSORT.QB4
< prev
next >
Wrap
Text File
|
1989-07-12
|
5KB
|
198 lines
' program: Seesort
' by John P. Grillo and J.D. Robertson
' Bentley College, Waltham, MA, 02154'
' from _More Color Computer Applications: High Resolution Graphics_'
' (New York, John Wiley & Sons, 1984)'
'
' Revision: 7-12-89 by Mike Welch of Dallas, TX
' This simple revision put the source code in QuickBASIC
' format in order to make the code more readable and
' a bit more structured.
DO ' begin the main loop of program
DEFINT A-Z ' Make integers the default
DIM X(390)
CLS
SCREEN 2
PRINT TAB(10); "Select"
PRINT TAB(12); "sort"
PRINT TAB(8); "technique"
PRINT TAB(50); "1 -- Exchange sort"
PRINT TAB(50); "2 -- Delayed exchange sort"
PRINT TAB(50); "3 -- Shell-Metzner sort"
PRINT TAB(50); "4 -- Shell sort"
PRINT TAB(50); "5 -- Insertion sort"
PRINT TAB(50); "6 -- End this program"
PRINT
PRINT TAB(40); "Which sort";
INPUT K
IF K = 6 THEN ' end the program
SCREEN 0, 0, 0
CLS
PRINT
PRINT "Program Terminated"
END
END IF
DO
LOCATE 11
PRINT TAB(40); "How many elements < 390";
INPUT N
LOCATE 11
PRINT STRING$(78, " ")
LOOP WHILE N > 390
PRINT
PRINT N; "elements in sort"
PRINT
DO
LOCATE 15
PRINT STRING$(40, " ")
LOCATE 15
PRINT "What kind of data?"
INPUT "Random or worst case (R/W)"; d$
d$ = UCASE$(d$) ' convert to uppercase
LOOP UNTIL d$ = "R" OR d$ = "W"
CLS
FOR I = 1 TO N
IF d$ = "W" THEN
X(I) = N + 1 - I
ELSE
X(I) = INT(RND * N + 1)
END IF
PSET (I, X(I))
NEXT I
ON K GOSUB ExchgSort, DelExchgSort, ShellMet, ShellSort, Insertion
LOOP ' loop indefinately
' _______________________________________________ '
' |...............................................| '
' |.... The following are gosubs--called from ....| '
' |.... the main loop of the program. ............| '
' |...............................................| '
' |_______________________________________________| '
Switch:
' *******************
' * Switch elements *
' *******************
T = X(I)
PRESET (I, T)
PSET (J, T)
X(I) = X(J)
PRESET (J, X(J))
PSET (I, X(J))
X(J) = T
RETURN
ExchgSort:
' ***********************
' **** Exchange sort ****
' ***********************
FOR I = 1 TO N - 1
FOR J = I + 1 TO N
IF X(I) > X(J) THEN
GOSUB Switch
END IF
NEXT J
NEXT I
RETURN
DelExchgSort:
' *******************************
' **** Delayed exchange sort ****
' *******************************
FOR P = 1 TO N - 1
X = P
FOR K = P + 1 TO N
IF X(K) < X(X) THEN
X = K
END IF
NEXT K
IF P <> X THEN
I = X
J = P
GOSUB Switch
END IF
NEXT P
RETURN
ShellMet:
' ****************************
' **** Shell-Metzner sort ****
' ****************************
P = N
ShellM1:
P = P \ 2
IF P = 0 THEN
RETURN
END IF
K = N - P
L = 1
ShellM2:
I = L
ShellM3:
J = I + P
IF X(I) < X(J) THEN
GOTO ShellM4
END IF
GOSUB Switch
I = I - P
IF I >= 1 THEN
GOTO ShellM3
END IF
ShellM4:
L = L + 1
IF L <= K THEN
GOTO ShellM2
END IF
GOTO ShellM1
ShellSort:
' ********************
' **** SHELL sort ****
' ********************
P = N
Shell1:
IF P <= 1 THEN
RETURN
END IF
P = P \ 2
M = N - P
Shell2:
F = 0
FOR J = 1 TO M
I = J + P
IF X(J) > X(I) THEN
GOSUB Switch
F = 1
END IF
NEXT J
IF F > 0 THEN
GOTO Shell2
END IF
GOTO Shell1
Insertion:
' ************************
' **** Insertion sort ****
' ************************
FOR P = 1 TO N - 1
T = X(P + 1)
FOR J = P TO 1 STEP -1
IF T > X(J) THEN
GOTO InserN1
END IF
X(J + 1) = X(J)
PSET (J + 1, X(J + 1))
PRESET (J, X(J + 1))
NEXT J
J = 0
InserN1:
X(J + 1) = T
PRESET (P + 1, T)
PSET (J + 1, T)
NEXT P
RETURN
END