Page 1 of 1

I need a sorting algorythm

Posted: Wed Nov 08, 2006 2:17 am
by bigbluehacker
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?

Posted: Sat Dec 30, 2006 6:41 am
by Dr_Davenstein
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    

Posted: Sat Jan 06, 2007 12:00 am
by bigbluehacker
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.

Posted: Sat Jan 06, 2007 12:48 am
by Ralph
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?

Posted: Sat Jan 06, 2007 5:48 am
by buff1
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)

Posted: Sat Jan 06, 2007 9:31 pm
by Ralph
I have to admit that Buff1's method is very good! Forget my suggestion.

Posted: Fri Jan 19, 2007 8:52 am
by Dr_Davenstein
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. ;)