Re: Sorting

 BBS: Inland Empire Archive
Date: 02-06-93 (03:57)             Number: 284
From: RICH GELDREICH               Refer#: NONE
  To: WAYNE VENABLES                Recvd: NO  
Subj: Re: Sorting                    Conf: (2) Quik_Bas
>   I badly need a good pointer sort routine for a program im writing,
> and I don't know every much about sorting... The routine needs to be
> able to sort integers, and it needs to a _pointer sort_ for the task i
> need it for, it also needs to be very very fast (This will stretch you
> skill to the limits!!)

    I use pointer sorts all the time, so perhaps I can help. (Some
people calls pointer sorts virtual sorts, also...). Here's some code off
the top of my head, which uses a very simplistic form of a Shell sort:

DIM Array(0 to MaxElement)
DIM Pointers(0 to MaxElement)

'Initialize pointers
FOR A=0 to MaxElement
    Pointers(A)=A
NEXT

Mid=MaxElement\2
DO
    DO
         S=0
         FOR A=0 to MaxElement-Mid
              IF Array(Pointers(A))>Array(Pointers(A+Mid)) then
                   Swap Pointers(A),Pointers(A+Mid)
                   S=-1
              END IF
         NEXT
    LOOP WHILE S
    Mid=Mid\2
LOOP WHILE Mid

    After the sort is complete, the first sorted element is
Array(Pointers(0)). The last element is Array(Pointers(MaxElement))).
This would print the sorted array:

FOR A=0 to MaxElement
    PRINT Array(Pointers(A));
NEXT

    This type of sort works great for string arrays, or when the
elements that must be sorted take a great deal of time to move around.

    Rich

--- MsgToss 2.0b
 * Origin: Computer Co-Op - Voorhees, NJ | Ted Hare (1:266/29)
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