No announcement yet.

fast sort

  • Filter
  • Time
  • Show
Clear All
new posts

  • fast sort

    This is a simple bubble sort. How can I make it sort faster.

    Changed% = 0
    FOR c% = 2 TO Matches%
    SEEK #OutFile%, ((c% - 2) * %QueryLen) + Offset%(KeyField%)
    GET$ #OutFile%, FldLen%(KeyField%), Buffer1$
    SEEK #OutFile%, ((c% - 1) * %QueryLen) + Offset%(KeyField%)
    GET$ #OutFile%, FldLen%(KeyField%), Buffer2$
    IF ucASE$(Buffer1$) > UCASE$(Buffer2$) THEN ' we need to swap these records
    Changed% = -1 ' we've made a change, so we'll have to make another pass through the file
    SEEK #OutFile%, (c% - 2) * %QueryLen
    GET #OutFile%, , Record1
    GET #OutFile%, , Record2
    ' swap the records
    SEEK #OutFile%, (c% - 2) * %QueryLen
    PUT #OutFile%, , Record2
    PUT #OutFile%, , Record1
    END IF
    NEXT c%
    LOOP WHILE Changed%
    ' the file is now sorted, so print the report


  • #2
    One fast suggestion: if the file can be loaded into a non-HUGE array, load it and do the swapping in memory rather than "in-place" on disk. Put the array back on disk when sorted.

    Depending on keysize and recordsize, it may work out better to load only an index, sort that and then put the records back into a new file.

    Or, you can change sort algorithms. I have PB/DOS code for an 'inplace' disk sort; I also have code for PB/DOS to sort a HUGE array.

    What recordsize, number of records and keylength are you looking at? The best design will be dependent on that.

    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]


    • #3
      if what michael suggests is possible, you could
      use the tag feature of array sort to sort the
      index and have what it points to "tag" along.

      even faster is my code you will find at
      Algorithms - interesting mathematical techniques with program code included.


      • #4
        Sort both up and down, decrementing the top limit and incrementing the bottom limit on each pass.
        I call this the Bubble and Brick sort.



        • #5
          The bubble sort compares and swaps consecutive pairs,
          passing through the entire list repeatedly until no swap
          is necessary.

          For this procedure to sort the list, the list must be
          scanned top to bottom on each pass.
          Algorithms - interesting mathematical techniques with program code included.