I need a sorting algorythm
Moderators:Administrator, Global Moderator
-
- Newbie
- Posts:8
- Joined:Wed Nov 08, 2006 1:57 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?
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?
-
- QBasic God
- Posts:166
- Joined:Tue Mar 25, 2003 12:45 am
- Location:U.S.A.
- Contact:
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...
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!
-
- Newbie
- Posts:8
- Joined:Wed Nov 08, 2006 1:57 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.
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.
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?
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.
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)
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)
-
- QBasic God
- Posts:166
- Joined:Tue Mar 25, 2003 12:45 am
- Location:U.S.A.
- Contact:
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!