M/C Part 17 BUBBLE SORT ROUTINE. I know there are better and faster sorting routines, but this is dead easy, and should still be speedier than Allan Moore oop the wing. It's also pretty easy to see how it works. Now, sorting routines have all sorts of uses. This particular one is taken from a WIMP thingy I'm doing for SAMCo where the disk directory is read and each file is represented by a 32 byte entry in a table. I needed to sort them into their various subdirectories and I then expanded the routine to allow extra sorting - alphabetically and/ or by filename extension. The details of the routine will depend on the type and size of data you want to sort, but let's assume my mini-directory sort. The files are stored, as I said, in 32 byte blocks congruently (wow, wotta word!) in memory with the file's subdirectory no. held in byte 22. I wanted them in ascending order. In a bubble sort we compare adjacent items and swap them to put them into the correct order. With n items in the list, you make (n-1) comparisons through the list the first time, and you always know that the last item is right (assuming you went forwards). In effect, the last term has ~bubbled~ its way through to the end. Because of this, the next time you only need to make (n-2) comparisons, the next (n-3) comparisons and so on. This makes a maximum of (n-1)(n-2)/2 comparisons, which ain't too bad. The bonus is that, if you don't make any swaps at all, the list is ordered and you can finish. So, we need a flag to be set every time we make a swap. If the flag isn't set after running through the list we can end. One final thing - since I'm swapping 32 byte blocks, a 32 byte buffer is needed to keep up speed. You copy the first block into the buffer, the second block onto the first, and the buffer onto the second. And that's it - dead easy (!) sort_dirs CALL dir_in ; Page in mini-dir to #8000 LD HL,(no_files) LD A,H OR L RET Z ; Return if there are less DEC HL ; than 2 files. LD A,H OR L RET Z LD (max_sort_count),HL LD B,H LD C,L ; Set up two counters - the count for moving through the list ; (BC), the other for repeating the loop, missing out one more ; on the end each time (max_sort_count). sd1 XOR A LD (swap_flag),A ; Reset the flag before a ; search. LD IX,#8000 LD HL,#8000 ; HL and IX always point to the same place - IX allows me to ; look 22 and 22+32 bytes ahead for the dir codes. HL is needed ; for the swap_dirs routine. #8000 is the start of the list. sd2 LD A,(IX+22+32) CP (IX+22) ; Compare consecutive dir codes. If there is a carry (ie. the ; second is lower than the first) swap the dirs. Note that, to ; save time, no swap is made if they are equal. CALL C,swap_dirs LD DE,32 ADD HL,DE ADD IX,DE ; Move on the pointers DEC BC LD A,B OR C JR NZ,sd2 ; Loop until (n-1) comparisons have been completed. LD A,(swap_flag) AND A RET Z ; Return if no swaps were made. LD HL,(max_sort_count) DEC HL LD (max_sort_count),HL LD B,H LD C,L ; Make one less comparison this time. LD A,H OR L JR NZ,sd1 RET no_files DW 0 max_sort_count DW 0 swap_flag DB 0 ; SWAP DIRS ROUTINE. This is used by other sorts so is ; generalised. Enter with HL pointing to first dir. ; Swaps adjacent dirs. swap_dirs PUSH AF PUSH BC PUSH DE PUSH HL LD A,1 LD (swap_flag),A LD DE,buffer LD BC,32 LDIR ; Dir 1 to buffer. POP DE PUSH DE LD BC,32 LDIR ; Dir 2 to dir 1. LD HL,buffer LD BC,32 LDIR ; Buffer to dir 2. POP HL POP DE POP BC POP AF RET buffer DS 32 Just a few points. Because MasterDOS allows over 700 files, the counters need to be 16 bit, but this makes it more flexible anyway. Dir_in just pages the mini directory into upper memory (the code sits over ROM0), but obviously you would adapt it. For descending order you would simply swap round the (IX+22+32) and (IX+22) at sd2. Easy peasy. .