Sort Routines

 BBS: Inland Empire Archive
Date: 02-25-94 (00:08)             Number: 228
From: DARYL GREENWOOD              Refer#: NONE
  To: JEFF COCHRAN                  Recvd: NO  
Subj: Sort Routines                  Conf: (2) Quik_Bas
*** Quoting Jeff Cochran to Dik Coates dated 02-19-94 ***
> DC>  JC> What is the fastest sort routine in normal QuickBASIC 4.5 for
> sortin
> DC>  JC> an array, alphabetic and numeric?
>
> DC> Depends on the data type... If the list is nearly sorted, the

This is part of a routine that I use! I create a
CUSTOMER.DAT file and a CUSTOMER.NDX. I don't sort the DAT
file, just the index file by phone number.
You could create multiple index files to search by other variables in the
CUSTOMER.DAT

Remember this is only the index file, the customer.dat
information is not needed for the example.

*************

TYPE IndexFile
PhoneNo AS STRING * 10
RecNo AS INTEGER ' This is the record number in the CUSTOMER.DAT
END TYPE
DIM I AS IndexFile

C.Phone = "8107910000"  ' This is a new phone number to add to my index!

    ' Load Customer.NDX into memory

    REDIM I(1 TO LenOfIndex) AS IndexFile
    DEF SEG = VARSEG(I(1))                         ' Find where the array
                                                   ' is located in memory

    BLOAD "\ORDER\DATA\CUSTOMER.NDX", VARPTR(I(1)) 'This file has to be
    DEF SEG                                        'created by bsave!


     ' Locate Position in index file to place new record
     ' This could be done better!
     Numb = 1     '  Numb = Record # to insert before
     Count = 1
     DO UNTIL Count > LenOfIndex
        IF VAL(C.Phone) < VAL(I(Count).PhoneNo) THEN
           ' I've found the record to insert BEFORE
           Numb = Count
           EXIT DO

           ELSE
           ' Still looking
           Count = Count + 1
         END IF
     LOOP


        ' Increase the size of the array by 1
        LenOfIndex = LenOfIndex + 1
        REDIM PRESERVE I(1 TO LenOfIndex) AS IndexFile

        ' Add the new Phone and record number to the end of the array
        I(LenOfIndex).PhoneNo = C.Phone
        I(LenOfIndex).RecNo = RecNo%


         IF Count = LenOfIndex THEN
         ' Dont sort - The new phone number was larger than any other
         ELSE
         ' Bubble Sort - Numb is the record number to insert before
            FOR X = LenOfIndex TO Numb + 1 STEP -1
              IF X > 1 THEN SWAP I(X), I(X - 1)
            NEXT X
         END IF

        ' Save the array
        DEF SEG = VARSEG(I(1))
        BSAVE "\ORDER\DATA\CUSTOMER.NDX", VARPTR(I(1)), LenOfIndex * LEN(I(1))
        DEF SEG

In a 3000 record database it take about 1 second to ADD the
new customer and Sort the Index file!


Daryl Greenwood
$$63
--- GEcho 1.00+
 * Origin: The Disk Box ][ Node 3 (313)790-6828 ZOOM 14.4K (1:2202/11)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET