Sorts

 BBS: Inland Empire Archive
Date: 12-08-92 (07:56)             Number: 382
From: DAVID COLSTON                Refer#: NONE
  To: ALL                           Recvd: NO  
Subj: Sorts                          Conf: (2) Quik_Bas
I have caught several post on sorts. In particular a need to sort a
directory. Here is a sample program for this in 7.1.
DECLARE SUB QuickSort (A$(), NumberofRecords!)
DIM A$(1000)
Count = 1
Filespect$ = "*.*"
A$(Count) = DIR$(Filespect$)
IF LEN(A$(1)) > 0 THEN
     DO
          A$ = DIR$
          IF LEN(A$) > 0 THEN
               Count = Count + 1
               A$(Count) = A$
          END IF
     LOOP UNTIL LEN(A$) = 0
ELSE
  Count = 0
END IF
IF Count > 0 THEN
    QuickSort A$(), Count
    OPEN "O", 2, "Files.Dir"
     FOR I = 1 TO Count
          PRINT #2, A$(I)
     NEXT
     CLOSE
END IF

SUB QuickSort (A$(), NumberofRecords) STATIC
False = 0
     Offset = NumberofRecords \ 2
     ' Loop until offset gets to zero:
     DO WHILE Offset > 0
          Limit = NumberofRecords - Offset
          DO

      ' Assume no switches at this offset:
      Switch = False

      ' Compare elements and switch ones out of order:
      FOR I = 1 TO Limit
           IF A$(I) > A$(I + Offset) THEN
                SWAP A$(I), A$(I + Offset)
                Switch = I
           END IF
      NEXT I

      ' Sort on next pass only to where
      ' last switch was made:
      Limit = Switch
          LOOP WHILE Switch

          ' No switches at last offset, try one half as big:
          Offset = Offset \ 2
     LOOP

END SUB


'I hope this helps some of you.

 * OLX 2.1 TD * Press any key to continue or any other key to quit


--- WM v2.04/92-0178
 * Origin: Paradox of Arkansas *Wildcat 3.0* 501-646-7158 (1:19/121)
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