'===========================================================================
' Subject: BUBBLE SORT                        Date: 01-01-96 (00:00)       
'  Author: Dave Gjessing                      Code: QB, QBasic, PDS        
'  Origin: dgjess@freenet.columbus.oh.us    Packet: ALGOR.ABC
'===========================================================================
'SORTER3.BAS    Dave Gjessing, 12/31/95, cleaned and remarked 1/1/96
'I wanted to figure out how to sort a random file before the year was out!
'This routine seems to do the job pretty darn well.
'-worked this out with playing cards on the kitchen table. I guess this is
'what is called a "bubble sort".

DEFINT A-Z
TYPE fullname
   firstN AS STRING * 20
   lastN AS STRING * 20
END TYPE
DIM SHARED entry AS fullname
'************************ start ******************************
menu:
CLS
PRINT "1 - run the sorter"
PRINT "2 - add records"
PRINT "3 - review file"
PRINT "K - kill the file"
PRINT "(Q)uit"

DO                      'get a keystroke
ans$ = INKEY$
IF ans$ = "1" THEN GOTO sortfile
IF ans$ = "2" THEN GOTO addrecs
IF ans$ = "3" THEN GOTO review
IF ans$ = "K" OR ans$ = "k" THEN KILL "sorter3.dat": GOTO menu
IF ans$ = "Q" OR ans$ = "q" THEN END
LOOP

'******************> the sorting routine <*********************
sortfile:
OPEN "sorter3.dat" FOR RANDOM AS #1 LEN = LEN(entry)
numrec = LOF(1) / LEN(entry)
DIM A AS fullname      'make a couple of distinct arrays to    
DIM B AS fullname      'hold TYPE fullname file entries in memory

FOR x = 1 TO numrec               'do the whole thing for as many
counter = 1                       'records as there are

FOR y = 1 TO numrec - 1           '(-1 or blank record is added)
 GET #1, counter, entry           'get the first (or next) record
  A = entry                       'put it in array A
   GET #1, counter + 1, entry     'get the second (or next + 1) record
    B = entry                     'put it in array B
    IF UCASE$(A.lastN) > UCASE$(B.lastN) THEN  'compare the arrays...

      'if the first is greater than the second, then switch their
      'places in the file, as follows.
       
        '(you may compare (and so sort by) *any* element of the array)
        '(the UCASE$ comparison is important; an uppercase letter has
        'a different ASCII value than it's lowercase counterpart!)

    entry = B                     'temporarily make entry = B
    PUT #1, counter, entry        'put that in A's former place (counter)
    entry = A                     'temporarily make entry = A
    PUT #1, counter + 1, entry    'put in B's former place (counter + 1)
    END IF
   counter = counter + 1          'bump the counter(s)
  NEXT y                          'do this for all of them
NEXT x                            'do *entire* process for all of them
CLOSE 1                           'done!
GOTO menu

'****************** get sample data ****************************
addrecs:
LINE INPUT "first name : "; entry.firstN
LINE INPUT "last name  : "; entry.lastN
OPEN "sorter3.dat" FOR RANDOM AS #1 LEN = LEN(entry)
numrec = LOF(1) / LEN(entry)
numrec = numrec + 1
PUT #1, numrec, entry
CLOSE 1
GOTO menu

'****************** look at the file ***************************
review:
CLS
PRINT "here is the file": PRINT
OPEN "sorter3.dat" FOR RANDOM AS #1 LEN = LEN(entry)
numrec = LOF(1) / LEN(entry)
counter = 1
FOR x = counter TO numrec
GET #1, counter, entry
PRINT entry.lastN; " "; entry.firstN
counter = counter + 1
NEXT x
PRINT : LINE INPUT "enter to continue"; junk$
CLOSE 1
GOTO menu
'************************* end *******************************

