SORTDOCM.TXT: Sort routine documentation. This routine is used for sorting (alphanumerically) any Random-access BASIC file. Random files have a key (ie the record number) which is numeric and is used for accessing the file. This routine enables the user to sort the file on any desired field within each record. This field may be any contiguous set of positions. Duplicate records (ie records with the same key) are discarded. The programs have been written with a fixed record length and number of records in the file, but this is easily changed (in BASIC) by the user. These, and other assumptions are listed below. There are three programs in the set: - SORTINIT.BAS which is used to manually build up a test file. The first 49 records are entered from the keyboard, the rest (records 50 to 256) are initialised with ASCII(256) in the first byte. - SORTPRNT.BAS which is used to print out the I/P or O/P test files. The user has to enter the file name. - SORTRNDM.BAS which is used to sort the input file and create a sorted output file. The sorting program consists of 4 sections: 1. Initialize: The files are opened and the output file is initialized with ASCII(256) in the first byte of every record. 2. Input: Each record is read in sequentially and a table is built up with 3 fields for each record. The "LT" (Less Than) fields chain records which are smaller than the previous one. The "GT" (Greater Than) fields chain records which are greater than the previous one. The "R" (Return) fields give the parent record in the chain. This is illustrated with the following example: Suppose 10 records are read in, in the order: D,B,C,E,A,G,F,I,K and H. The table below shows the contents of the chain fields at the end of the input section. I/P I LT GT R -------I----------- 1 D I 2 4 . 2 B I 5 3 1 3 C I . . 2 4 E I . 6 1 5 A I . . 2 6 G I 7 8 4 7 F I . . 6 8 I I 10 9 6 9 K I . . 8 10 H I . . 8 I 3. Output: The table is used to write records beginning with the record with the smallest key. The chain fields are followed until all records are written. 4. Closedown: The files are closed and an end-of-program message is displayed. If required, this section could be used to "kill" the input file and rename the output file. Assumptions: ----------- * Assumes that the input file is Random Access, and that the output file does NOT exist! * Assumes that data records are present in the input file until Hex FF, ie ASCII(256) is found in the first byte of a record. It assumes that there are no records following this. * Assumes the maximum number of records is 256. This can be easily altered by changing the following statements: 1110 DIM T$(256),... - change the size of all the variable arrays to = the maximum number of records. 1160 FOR I%=1 TO 256 - change the end value of I to = the maximum number of records. 2090 IF N%>256 GOTO 2230 - change the comparison value to = the maximum number of records * The input file-name is INPUTFIL.IND (see line 1060). The program is written with a file length of 32, but this can be altered by changing lines 1060 to 1090. The field to be sorted is from position 11 to 18 (8 bytes), but this can also be easily changed, again in the lines 1060 to 1090. The names of the variables should be kept as they are: Part 1 of the record is IX1$ (nominally 10 bytes) Part 2 (the sort field) IS$ (nominally 8 bytes) Part 3 of the record is IX2$ (nominally 14 bytes). * The output file-name is OUTPUTFL.SRT (see line 1080). The field length definitions (line 1090) should be an exact copy of the input file definitions.