Page 1 of 1

Sorting alphabetically help

Posted: Thu Nov 24, 2005 11:16 am
by Guest
I have a qbasic program that needs to be able to read names from a data file, print the names, then sort and print the names alphabetically. After the names are sorted, it outputs the names sorted alphabetically to a data file. Lastly, the program performs a search where the user enters a name to search and the program outputs if the name entered was found in the list of names or not.

That?s an overview of what the program does, however I am stuck on one particular part and was hoping I can get some help here. I have everything working in the program as I want, except the sorting alphabetically section. To me it looks like the logic is correct in B020SortArray, however when the program prints the list of sorted names their not sorted alphabetically.

Here is the program logic if anyone would be willing to help me out:

Code: Select all

DECLARE SUB B000InputArray ()
DECLARE SUB B010PrintArray (strTitle AS STRING)
DECLARE SUB B020SortArray ()
DECLARE SUB B030WriteArray ()
DECLARE SUB B040SearchArray ()
CLS
CONST intSIZE = 15
CONST intRow = 15
DIM SHARED strNames(intSIZE) AS STRING
DIM SHARED intPageCount AS INTEGER
DIM SHARED NUM(1 TO 26)
CALL B000InputArray
CALL B010PrintArray("UNSORTED NAMES LISTING")
CALL B020SortArray
CALL B010PrintArray("SORTED NAMES LISTING")
CALL B030WriteArray
CALL B040SearchArray
END

SUB B000InputArray
DIM intSubscript AS INTEGER
OPEN "names.txt" FOR INPUT AS #1
intPageCount = 1
intSubscript = 1
DO WHILE intSubscript <= intSIZE
        INPUT #1, strNames(intSubscript)
        intSubscript = intSubscript + 1
LOOP
CLOSE #1
END SUB

SUB B010PrintArray (strTitle AS STRING)
DIM strNewPage AS STRING
DIM intSubscript AS INTEGER
PRINT
PRINT "Press enter to see page"; intPageCount
INPUT strNewPage
CLS
PRINT strTitle, "Page"; intPageCount
PRINT
intPageCount = intPageCount + 1
intSubscript = 1

DO WHILE intSubscript <= intSIZE
        PRINT strNames(intSubscript)
                intSubscript = intSubscript + 1
LOOP

END SUB

SUB B020SortArray
DIM strPass AS INTEGER
DIM intSubscript AS INTEGER
DIM sngTemp AS SINGLE
NUM(1) = A
NUM(2) = B
NUM(3) = C
NUM(4) = D
NUM(5) = E
NUM(6) = F
NUM(7) = G
NUM(8) = H
NUM(9) = i
NUM(10) = J
NUM(11) = K
NUM(12) = L
NUM(13) = M
NUM(14) = N
NUM(15) = O
NUM(16) = P
NUM(17) = Q
NUM(18) = R
NUM(19) = S
NUM(20) = T
NUM(21) = U
NUM(22) = V
NUM(23) = W
NUM(24) = X
NUM(25) = Y
NUM(26) = Z
FOR strPass = 1 TO 25
        FOR i = 1 TO 26 - strPass
                IF NUM(i) > NUM(i + 1) THEN
                        SWAP NUM(i), NUM(i + 1)
                        END IF
                        
                NEXT i
        NEXT strPass
END SUB

SUB B030WriteArray
DIM intSubscript AS INTEGER
OPEN "namesort.txt" FOR OUTPUT AS #2
intSubscript = 1
DO WHILE intSubscript <= intSIZE
        WRITE #2, strNames(intSubscript)
        intSubscript = intSubscript + 1
LOOP
CLOSE #2
END SUB

SUB B040SearchArray
DIM strNameID AS STRING
DIM intSubscript AS INTEGER
DIM strFound AS STRING
INPUT "Enter a name to search, use the format: Last, First. Use quotes. ", strNameID
intSubscript = 1
strFound = "NO"
DO WHILE intSubscript <= intSIZE AND strFound = "NO"
        IF strNameID = strNames(intSubscript) THEN
                PRINT "NAME FOUND"
                PRINT strNameID, strNames(intSubscript)
                strFound = "YES"
        END IF
        intSubscript = intSubscript + 1
LOOP
IF strFound = "NO" THEN
        PRINT strNameID; " NAME NOT FOUND"
END IF
END SUB
I am also wondering if there is a more efficient way to do this(found in B020SortArray):

Code: Select all

NUM(1) = A
NUM(2) = B
NUM(3) = C
NUM(4) = D
NUM(5) = E
NUM(6) = F
NUM(7) = G
NUM(8) = H
NUM(9) = i
NUM(10) = J
NUM(11) = K
NUM(12) = L
NUM(13) = M
NUM(14) = N
NUM(15) = O
NUM(16) = P
NUM(17) = Q
NUM(18) = R
NUM(19) = S
NUM(20) = T
NUM(21) = U
NUM(22) = V
NUM(23) = W
NUM(24) = X
NUM(25) = Y
NUM(26) = Z
Thanks in advance for any help.

Posted: Thu Nov 24, 2005 11:24 am
by Guest
in my database program after the person is done making the data file
we did this...for WINDOWS


SHELL "sort.exe path to file /o path to new file"

then a person could print off the file to paper or screen
in a sorted manner...

Posted: Thu Nov 24, 2005 12:11 pm
by {Nathan}
I am NOT good at sorting but I can tell you a more efficiant way than doing the NUM(1) = A... BLAH BLAH BLAH!!! OK, I am assuming you want it in a string value (like it should be "A") if so, then do this:

Code: Select all

FOR Number = 1 to 26
NUM(Number) = CHR$(64+Number)
NEXT Number
That should put the value "A", then "B" ect into the array. If you want the ASCII value of it, then do this:

Code: Select all

FOR Number = 1 to 26
NUM(Number) = 64+Number
NEXT Number
And, that is also assuming that you want the upper case value. If you want lower case, then change the 64 to a 95. (Check the table at http://www.lookuptables.com/ to know where I got the 64 and 95 from.)

Posted: Thu Nov 24, 2005 12:59 pm
by Guest
Thanks Nathan1993 and "Guest", that helps allot :D

So I got my sort array cleaned up, here it is:

Code: Select all

DIM intPass AS INTEGER

FOR Number = 1 TO 26
NUM(Number) = 64 + Number
NEXT Number

FOR intPass = 1 TO 25
        FOR i = 1 TO 26 - intPass
                IF NUM(i) > NUM(i + 1) THEN
                        SWAP NUM(i), NUM(i + 1)
                        END IF
                        
                NEXT i
        NEXT intPass
END SUB
Looks cleaner, now I just need to figure out why it isn't sorting...

Thanks again for the help.

Posted: Thu Nov 24, 2005 7:02 pm
by moneo
I don't know why your sort is not working, but it doesn't really matter because what you want to sort are the names in the strNames array and not the ASCII values of the letters A-Z in the NUM array.
Do you agree?

Let's be clear what we want to sort, and then work on a sort routine.
*****

Posted: Fri Nov 25, 2005 10:30 am
by Guest
Correct moneo, what I want to sort is the names in the strNames array. I guess I?m just not sure how to approach this. Thanks for the clarification in this matter.

Posted: Fri Nov 25, 2005 2:13 pm
by moneo
Anonymous wrote:Correct moneo, what I want to sort is the names in the strNames array. I guess I?m just not sure how to approach this. Thanks for the clarification in this matter.
Since you only have a handful of names to be sorted, then I recommend the simplest sort routine possible, which is the Bubble Sort. You can spend a lot of time looking for and debugging more sophisticate sort algorithms, but for a limited number of elements to sort and the speed of today's machines, the difference is negligible and not worth the effort.

Below is the simplest implementation of the Bubble Sort that I could find in my stuff. It was written by the famous Ethan Winer.

1) Notice the UBOUND function. This supplies the largest subscript value of the array. This save you from having to put the number in, like intSIZE.

2)I left the original code alone, so you need to substitute strNames for array$.

Code: Select all

DO
  OutOfOrder = 0                          'assume it's sorted
  FOR X = 1 TO UBOUND(Array$) - 1
      IF Array$(X) > Array$(X + 1) THEN   'Compare adjaccent elements 
         SWAP Array$(X), Array$(X + 1)    'if we had to swap,
         OutOfOrder = 1                   '   then we're not done yet. 
      END IF
  NEXT
LOOP WHILE OutOfOrder = 1
Let me know if you have any problems with this.
Also let me know I you don't understand the code, and I'll send you some documentation on it.
*****

Posted: Sat Nov 26, 2005 12:02 pm
by Guest
moneo, I truly appreciate your help :D

Thank you so much, thanks to you I now understand the bubble sort, plus my program is finally working.

Thank you so much.

Posted: Sat Nov 26, 2005 1:50 pm
by Guest
Anonymous wrote:moneo, I truly appreciate your help :D

Thank you so much, thanks to you I now understand the bubble sort, plus my program is finally working.

Thank you so much.
You're quite welcome.

Why don't you register and become a member of the forum. It's a bit awkward addressing posts to a totally unknown person. You'll find that besides offering you help, the forum covers very interesting subjects in the art of QBasic programming.
*****