# Alphabetizer FUNCTION

``` BBS: Inland Empire Archive
Date: 12-07-92 (22:52)             Number: 306
From: JOE NEGRON                   Refer#: NONE
Subj: Alphabetizer FUNCTION          Conf: (2) Quik_Bas```
```SH> Does anyone have a FUNCTION that that will alphabatize a complete
> string array? I have tried unsucessfuly. I'm sure I could do it, but I
> thought I would check with you guys to see if you had one lying around.

What you need is a sort routine.  There are a number of different
sorting methods; here is a relatively simple one, based on the Bubble
Sort, called a "Comb" sort:

============================== Begin code ==============================
DEFINT A-Z

DECLARE SUB CombSort (Array\$())

'***********************************************************************
'* SUB CombSort
'*
'* PURPOSE
'*    Sorts an array using the Comb sort algorithm.
'*
'* CREDIT(S)
'*    This routine was taken from the article "A Fast, Easy Sort", by
'*    Stephen Lacey and Richard Box, from the "Hands On" column of the
'*    April 1991 issue of Byte.
'***********************************************************************
SUB CombSort (Array\$()) STATIC
FirstEl% = LBOUND(Array\$)
LastEl% = UBOUND(Array\$)
Gap% = LastEl%

DO
IF Gap% * 8 / 11 > 1 THEN
Gap% = Gap% * 8 / 11
ELSE
Gap% = 1
END IF

Switch% = 0

FOR I% = FirstEl% TO LastEl% - Gap%
J% = I% + Gap%
IF Array\$(I%) > Array\$(J%) THEN
SWAP Array\$(I%), Array\$(J%)
Switch% = Switch% + 1
END IF
NEXT I%
LOOP UNTIL Switch% = 0 AND Gap% = 1
END SUB
=============================== End code ===============================

Simply pass your array to this SUB as follows:

CombSort Array\$()

or

CALL CombSort(Array\$())

As I said, there are a number of different sorting algorithms.  I use
this one the majority of the time since it is small, simple, and fairly
fast.

Just for comparison's sake, here is a Shell sort:

============================== Begin code ==============================
DEFINT A-Z

DECLARE SUB ShellSort (Array\$())

'***********************************************************************
'* SUB ShellSort
'*
'* PURPOSE
'*    Sorts an array using the shell sort algorithm.
'***********************************************************************
SUB ShellSort (Array\$()) STATIC
FirstEl% = LBOUND(Array\$)
LastEl% = UBOUND(Array\$)
Span% = LastEl% \ 2

DO WHILE Span% > 0
Boundary% = LastEl% - Span%

DO
Flag% = 0

FOR I% = FirstEl% TO Boundary%
IF Array\$(I%) > Array\$(I% + Span%) THEN
SWAP Array\$(I%), Array\$(I% + Span%)
Flag% = I%
END IF
NEXT I%

Boundary% = Flag% - Span%
LOOP WHILE Flag%

Span% = Span% \ 2
LOOP
END SUB
=============================== End code ===============================

Both of these routines are fairly fast, and require a minimal amout of
memory.

BTW, these routines are implemented as SUBs rather than FUNCTIONs,
because they do not need to return a value to the caller.

--Joe in Bay Ridge, Brooklyn, NY--
Mon  12-07-1992, 22:38

* SLMR 2.1a * Windows: Brought to you by the makers of Edlin!

--- Maximus 2.01wb
* Origin: * BlueDog BBS * (212) 594-4425 * NYC FileBone Hub (1:278/709)
```

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