home *** CD-ROM | disk | FTP | other *** search
- BASIC Sort Using A RAM Disk
-
- Fred Westendarp
- Phoenix IBM PC Users Group
-
- Sorting lists is a common programming
- task. This can be a problem when
- using BASIC because of the memory
- limitation (64K) placed on BASIC's
- workspace. For example, a list of
- 4000 items, each representing a
- string of 20 characters, would
- require (2500 x 20) = 80K bytes of
- array space! This does not consider
- the room required for the program
- itself, and sorting tasks of larger
- proportions are not uncommon. One
- solution is to leave the data on the
- disk instead of loading it into an
- array in BASIC memory. No array space
- would be required, and a list of
- items filling an entire disk could be
- sorted (360K). This concept brings
- larger sorting tasks into the realm
- of possibility. However, sorting to a
- disk has its drawbacks: the number of
- disk reads and writes required is
- objectionable and will have your
- drive unit smoking (figuratively) in
- no time. But worst of all, sorting to
- a disk is slow!
-
- One solution is to sort to a RAM
- disk. The data is in the PC memory,
- so no time consuming mechanical disk
- access is required. In addition, the
- data is outside of BASIC`S 64K
- program area, leaving plenty of room
- for your program.
-
- The basic procedure is to first
- create a RAM disk. In the following
- example, I create RAM disk C:,
- consisting of 160K bytes, prior to
- booting BASIC. Step two is to create
- a file on disk C: which contains the
- data to be sorted. This file could be
- called C:ARRAYFILE. Next, load the
- file with data from your floppy disk,
- sort the data on the RAM disk, then
- write it back to the floppy.
-
- The following example assures the
- existence of the RAM disk C:, and a
- file called NAMES.DAT on drive A:.
- The NAMES.DAT file consists of 4000
- records, each 20 bytes in length,
- containing a list of names. The names
- are in random order, and the task is
- to alphabetize them! This represents
- 89K worth of data, and could not be
- loaded into an array in BASIC's
- memory.
-
- 1 ' * * Program: DEMO.BAS
- Includes demo of RAM-SORT *
- 2 ' * * Requires: RAM Disk C: of
- size adequate to hold array to *
- 3 ' be sorted
- 4 ' * * Fred Westendarp / Tempe,
- AZ / January,1984 *
- 5 '
- 10 OPEN "A:NAMES.DAT" AS #1 LEN=20
- 'open for random access.
- 20 FIELD #1,20 AS NAM$
- 'field the buffer.
- 30 OPEN "C:ARRAYFILE" AS #2 LEN=20
- 40 FIELD #2,20 AS ARF$
- 50 FOR LOOP=1 TO 4000
- 'for each data record:
- 60 GET #1,LOOP
- 'get it off the floppy,
- 70 LSET ARF$=NAM$
- 'set to buffer,
- 80 PUT #2,LOOP
- 'and write it to RAM disk.
- 90 NEXT LOOP
- 100 ITEMS=4000 : GOSUB 500
- p2 'RAM-SORT Subroutine
- 110 CLOSE #1
- 'leave A:NAMES.DAT intact,
- 120 OPEN "A:NAMES.SOR" AS #1 LEN=20
- 'and write sorted list to new file.
- 130 FIELD #1,20 AS NAM$
- 140 FOR LOOP=1 TO 4000
- 'for each record:
- 150 GET #2,LOOP
- 'get record from C:ARRAYFILE,
- 160 LSET NAM$=ARF$
- 'set to buffer
- 170 PUT #1,LOOP
- 'and write it to floppy disk.
- 180 NEXT LOOP
- 190 CLOSE
- 'close the disk files
- 200 END
- 499 '= = = = = = = = = = = RAM-SORT
- SUBROUTINE (Shell-Metzner) = = = = =
- 500 NN = ITEMS + 1
- 'ITEMS=count passed from above.
- 510 M = NN
- 'other sort routines could
- 520 M = INT(M/2)
- 'be substituted for this one.
- 530 IF M = O THEN 710
- 540 KK = NN - M
- 550 J =1
- 560 I = J
- 570 LL = I + M
- 580 GET #2,I : AR1$ = ARF$
- 590 GET #2,LL : AR2$ = ARF$
- 600 IF AR1$ <= AR2$ THEN 680
- 610 ART$ = AR1$
- 620 AR1$ = AR2$
- 630 AR2$ = ART$
- 640 LSET ARF$ = AR1$ : PUT #2,I
- 650 LSET ARF$ = AR1$ : PUT #2,I
- 660 I = I - M
- 670 IF I >= 1 THEN 570
- 680 J = J + 1
- 690 IF J = KK THEN 520
- 700 GOTO 560
- 710 RETURN