I need a sorting algorythm

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
bigbluehacker
Newbie
Posts:8
Joined:Wed Nov 08, 2006 1:57 am
I need a sorting algorythm

Post by bigbluehacker » Wed Nov 08, 2006 2:17 am

I need an algorythm to sort one field of an array while preserving the relationship with all other fields on the same line. For example:

1 Man-O-War $3,967,845
2 Swaps $2,897,136
3 Secretariat $10,145,873
4 Sunday Silence $265,972

These are fictional examples; I need to sort winnings by number; don't need the name. result would look like:

4 265,972
2 2,897,136
1 3,967,845
3 10,145,873

Years ago I had such a routine written in Commodore Basic and have long since lost it. It was about 20 lines long. I haven't been able to find such a routine for QBasic and have searched everywhere. Does anyone know of one?

Dr_Davenstein
QBasic God
Posts:166
Joined:Tue Mar 25, 2003 12:45 am
Location:U.S.A.
Contact:

Post by Dr_Davenstein » Sat Dec 30, 2006 6:41 am

Hmmm... Do you mean you have different fields stored in a linear fashion within the same array?

Anyway, the easiest type of sort to learn, is the bubble sort. It's basically like this...

Code: Select all

Dim Array(20) As Integer

For i = LBound(Array) To UBound(Array)
    y=y+1
    Array(i) = Int(Rnd*1000)
    Print Array(i)
Next



For i = LBound(Array) To UBound(Array)
    For j = i To UBound(Array)
        If Array(i)>=Array(j) Then
            Swap Array(i), Array(j)
        End If
    Next
Next

y=0
For i = LBound(Array) To UBound(Array)
    y=y+1
    Locate y,10
    Print Array(i)
Next    
Come check out [url=http://www.freebasic.net/forum/index.php]FreeBASIC[/url]. The syntax is based on QuickBasic, but it expands to use pointers, operator overloading, etc... The list goes on and on!

bigbluehacker
Newbie
Posts:8
Joined:Wed Nov 08, 2006 1:57 am

Post by bigbluehacker » Sat Jan 06, 2007 12:00 am

Thank you for your reply.

I have tried to use "bubble" and other simple sorts and can't get them to work correctly with 2 or 3 dimensional arrays. If DIM Array(1 to 20,1 to 20) AS LONG I can sort elements in one of the dimensions, but the other dimension remains unaltered.

If I dimension the array as Sales(1 to 200, 1 to 200) as string (because the first dimension is names and the second is numbers), how can I sort the sales amounts from high to low and read the correct salesperson's name next to each amount?

Thanks again for your input. Any other suggestions will be welcome.

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Sat Jan 06, 2007 12:48 am

How about concatinating field 3 with a spacer and with field 1? Then, you can sort the new, concatenated values, after which you would again separate the components, by using the LEFT$, MID$, AND RIGHT$, as covenient.

Do you see the logic?
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

buff1
Jr. Member
Posts:19
Joined:Mon Oct 30, 2006 4:37 pm
Contact:

Post by buff1 » Sat Jan 06, 2007 5:48 am

Set up 3 arrays.
test data.
1 Man-O-War $3,967,845
2 Swaps $2,897,136
3 Secretariat $10,145,873
4 Sunday Silence $265,972

array1$(1)="1":array1$(2)="2" etc
array2$(1)="Man-O-War":array2$(2)="Swaps" etc.
array3$(1)="3967845":array3$(2)="2987136" etc.

then sort the 'array' that you want sorted and use swap to swap the
other array with the same item numbers

i.e. if array1$ is used for the sort
then when you
swap array1$(j),array1$(k)
you also
swap array2$(j),array2$(k)
swap array3$(j),array3$(k)

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Sat Jan 06, 2007 9:31 pm

I have to admit that Buff1's method is very good! Forget my suggestion.
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Dr_Davenstein
QBasic God
Posts:166
Joined:Tue Mar 25, 2003 12:45 am
Location:U.S.A.
Contact:

Post by Dr_Davenstein » Fri Jan 19, 2007 8:52 am

Does QB allow you to swap whole UDT's of the same TYPE? I can't remember. That would be the best approach, in my opinion. If that doesn't work, I would definetly go with buff1. ;)
Come check out [url=http://www.freebasic.net/forum/index.php]FreeBASIC[/url]. The syntax is based on QuickBasic, but it expands to use pointers, operator overloading, etc... The list goes on and on!

Post Reply