5 GRAPHICS 0 10 MAXVALS=1000:DIM VALUES(MAXVALS) 20 DIM INFILE$(11):INFILE$="D:INPUT.TXT" 30 DIM OUTFILE$(12):OUTFILE$="D:OUTPUT.TXT" 40 PRINT "����������Ԡ����ɠ����ê��" 50 GOSUB 2000 60 GOSUB 4000 70 GOSUB 3000 80 PRINT "�����Π�������Ū��" 90 PRINT "HEAPSORT TOOK ";ELAPSED;" SECONDS." 999 END 1000 REM Restore the max-heap property below ROOT within HEAPSIZE elements. 1001 REM Inputs: ROOT is a zero-based root index; HEAPSIZE is the exclusive end. 1002 REM Output: VALUES is updated in place; this GOSUB routine returns via RETURN. 1010 CHILD=ROOT*2+1 1020 IF CHILD>=HEAPSIZE THEN GOTO 1120 1030 IF CHILD+1>=HEAPSIZE THEN GOTO 1060 1040 IF VALUES(CHILD)>=VALUES(CHILD+1) THEN GOTO 1060 1050 CHILD=CHILD+1 1060 IF VALUES(ROOT)>=VALUES(CHILD) THEN GOTO 1120 1070 TEMP=VALUES(ROOT) 1080 VALUES(ROOT)=VALUES(CHILD) 1090 VALUES(CHILD)=TEMP 1100 ROOT=CHILD 1110 GOTO 1010 1120 RETURN 1130 REM ��������������� 2000 REM Read integer lines from INFILE$ into VALUES and update COUNT. 2001 REM Input: INFILE$ path. Output: VALUES and COUNT; returns via RETURN. 2010 PRINT "READING INPUT FILE..."; 2020 DIM L$(80) 2030 OPEN #1,4,0,INFILE$ 2040 TRAP 2100 2050 INPUT #1,L$ 2060 IF L$="" THEN GOTO 2040 2070 VALUES(COUNT)=VAL(L$) 2080 COUNT=COUNT+1 2090 GOTO 2040 2100 CLOSE #1 2110 TRAP 65535 2120 PRINT "DONE." 2130 RETURN 2140 REM ��������������� 3000 REM Write VALUES(0..COUNT-1) to OUTFILE$, one value per line. 3001 REM Inputs: OUTFILE$, VALUES, and COUNT. Returns via RETURN. 3010 PRINT "SAVING OUTPUT FILE..."; 3020 OPEN #2,8,0,OUTFILE$ 3030 FOR I=0 TO COUNT-1 3040 PRINT #2,STR$(VALUES(I)) 3050 NEXT I 3060 CLOSE #2 3070 PRINT "DONE." 3080 RETURN 3090 REM ��������������� 4000 REM Heap-sort VALUES(0..COUNT-1) in place and set ELAPSED seconds. 4001 REM Inputs: VALUES and COUNT. Output: sorted VALUES and ELAPSED; returns via RETURN. 4010 PRINT "SORTING ELEMENTS..."; 4020 STTIME=(PEEK(18)*65536+PEEK(19)*256+PEEK(20))/60 4030 HEAPSIZE=COUNT 4040 START=INT(HEAPSIZE/2)-1 4050 IF START<0 THEN GOTO 4100 4060 ROOT=START 4070 GOSUB 1000 4080 START=START-1 4090 GOTO 4050 4100 LAST=HEAPSIZE-1 4110 IF LAST<=0 THEN GOTO 4200 4120 TEMP=VALUES(0) 4130 VALUES(0)=VALUES(LAST) 4140 VALUES(LAST)=TEMP 4150 HEAPSIZE=LAST 4160 ROOT=0 4170 GOSUB 1000 4180 LAST=LAST-1 4190 GOTO 4110 4200 ENTIME=(PEEK(18)*65535+PEEK(19)*256+PEEK(20))/60 4210 PRINT "DONE." 4220 ELAPSED=ENTIME-STTIME 4230 IF ELAPSED<0 THEN ELAPSED=ELAPSED+86400 4240 RETURN 4250 REM ���������������