| Author |
Message |
|
LoneStar
Commander
Joined: Fri Jun 09, 2006 2:00 am Posts: 1401 Location: Canada
|
Singularity wrote: A bubble sort is a horrible, horrible algorithm. It is taught in college only because the professor is a newb, or merely addicted to bad first-year algorithms. Use a radix or bucket sort, it is far far faster, n*log(n) as opposed to n^2.
Did a quick read on the subject. The Bucket sort looks like a glorified Bubble sort. I believe can still be done within a single array.
The Radix sort is interesting. I wonder how would one could easily obtain LSD, or MSD with TWX?
How could I sort:
170, 45, 75, 90, 2, 24, 802, 66
into:
170, 90, 2, 802, 24, 45, 75, 66
..etc.
P.S. I'm not trying to be argumentative. Am genuinely curious.
_________________ ---------------------------- -= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
|
| Thu Apr 12, 2007 6:52 am |
|
 |
|
Singularity
Veteran Op
Joined: Thu Jun 02, 2005 2:00 am Posts: 5558 Location: USA
|
Uhm, well it's not a glorified bubble sort... laff. A radix sort is a form of bucket sort.
An in-place algorithm is one that does it's job on the data being processed, without creating a second array.
LSD is not in-place, IIRC, MSD can be... but it will be tricky to handle the recursion. EP has managed to use stacks to replace the local namespace that's needed. It'll take a little work to adapt it to TWX, but I think it can be done. The benefit here is that as the list grows, the time required to sort the list is much much smaller. On really small lists I doubt it matters, but if you're working with 1000+ elements... it's going to make a big difference.
Neither of your 2 lists are sorted. The 2nd is only a single pass on (I think) an LSD version. You'd use getLength and cutText I guess.
But for a list that small, with a spread of less than 1000 a simple counting sort would be smarter... Make an $occurances array as you process the data that has how many times a particular value occurs. Then increment from the min to the max, and if there's an occurance then write it. This works great whenever the spread between min and max is relatively small, but as it gets larger it gets much less efficient. A counting sort is not in-place but with such a small dataset it hardly matters.
_________________ May the unholy fires of corbomite ignite deep within the depths of your soul...
1. TWGS server @ twgs.navhaz.com 2. The NavHaz Junction - Tradewars 2002 Scripts, Resources and Downloads 3. Open IRC chat @ irc.freenode.net:6667 #twchan 4. Parrothead wrote: Jesus wouldn't Subspace Crawl.
*** SG memorial donations via paypal to: dpocky68@booinc.com
|
| Thu Apr 12, 2007 7:43 am |
|
 |
|
LoneStar
Commander
Joined: Fri Jun 09, 2006 2:00 am Posts: 1401 Location: Canada
|
Singularity wrote: Uhm, well it's not a glorified bubble sort... laff. A radix sort is a form of bucket sort.
An in-place algorithm is one that does it's job on the data being processed, without creating a second array.
LSD is not in-place, IIRC, MSD can be... but it will be tricky to handle the recursion. EP has managed to use stacks to replace the local namespace that's needed. It'll take a little work to adapt it to TWX, but I think it can be done. The benefit here is that as the list grows, the time required to sort the list is much much smaller. On really small lists I doubt it matters, but if you're working with 1000+ elements... it's going to make a big difference.
Neither of your 2 lists are sorted. The 2nd is only a single pass on (I think) an LSD version. You'd use getLength and cutText I guess.
The second list is the result of a 1st pass LSD. Based on what I've read, the second pass would focus on the 'tens' column, and so on. However, without bitwise operators or even a MOD operator, looks like Bubble-type sorts win at the end of the day --as far as TWX is concerned.
_________________ ---------------------------- -= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
|
| Thu Apr 12, 2007 7:57 am |
|
 |
|
Singularity
Veteran Op
Joined: Thu Jun 02, 2005 2:00 am Posts: 5558 Location: USA
|
Bubble sorts are n^2 time, radix is n*log(n) @ worst-case. Do the math, you don't need bitwise operators, all you need is an intelligent way to get a digit. With that said I think TWX has bitwise math, doesn't it? Couldn't you do a bitmask with XOR?
_________________ May the unholy fires of corbomite ignite deep within the depths of your soul...
1. TWGS server @ twgs.navhaz.com 2. The NavHaz Junction - Tradewars 2002 Scripts, Resources and Downloads 3. Open IRC chat @ irc.freenode.net:6667 #twchan 4. Parrothead wrote: Jesus wouldn't Subspace Crawl.
*** SG memorial donations via paypal to: dpocky68@booinc.com
|
| Thu Apr 12, 2007 8:25 am |
|
 |
|
Parrothead
Commander
Joined: Wed May 03, 2006 2:00 am Posts: 1722 Location: USA
|
ok ill bite here is a script I thru together to clean the Major Space Lane list of duplicates and line them up in numerical value low to high.
Code: SetVar $mslfile (GAMENAME &"-"& GAME & "-msl.txt") setarray $mslsectors SECTORS readtoarray $mslfile $msl setvar $i 1 while ($i <= $msl) setvar $temp $msl[$i] isnumber $numck $temp if ($numck = 1) if ($temp <> "0") setvar $mslsectors[$temp] 1 end end add $i 1 end delete $mslfile setdelaytrigger one ne 1500 pause ne setvar $i 1 while ($i <= SECTORS) if ($mslsectors[$i] = 1) write $mslfile $i end add $i 1 end halt
I would be interested on how to do this without ending up with unwanted aarrays in memory. Speed not an issue on something that gets run once a game but its as good an example as any.
|
| Thu Apr 12, 2007 1:55 pm |
|
 |
|
LoneStar
Commander
Joined: Fri Jun 09, 2006 2:00 am Posts: 1401 Location: Canada
|
Singularity wrote: Bubble sorts are n^2 time, radix is n*log(n) @ worst-case. Do the math, you don't need bitwise operators, all you need is an intelligent way to get a digit. With that said I think TWX has bitwise math, doesn't it? Couldn't you do a bitmask with XOR?
Forgive me. I can see clearly the speed advantage of a Radix sort over a Buble type.. my previous post simply stated that Radix sorts in TWX are not easily attainable and probably not even worth the effort. Using bitwise operators *is* an intelligent method. Case and point
Code: 1010 10 1101 13 0010 02 1111 15
use a bit mask of 0001, sort and you get 1010 10 0010 02 1101 13 1111 15
use a bit mask of 0010, sort and you get 1101 13 1010 10 0010 02 1111 15
use a bit mask of 0100, sort and you get 1010 10 0010 02 1101 13 1111 15
use a bit mask of 1000, sort and you get 0010 02 1010 10 1101 13 1111 15
and volla, a radix sort. First, TWX doesn't allow Logical expressions with XOR it only allows $SomeVar to be XOR'd with TRUE or FALSE. Besides. What's needed to achive a bitwise Radix sort is a Logical AND.
I'm not an expert by any means. But manipulating data with a mish mash of String functions resulting in mixing Data Types (Long Int -> unsigned Char -> Long Int) cannot ever be more efficient than dealing at the Binary Level.
_________________ ---------------------------- -= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
|
| Thu Apr 12, 2007 3:09 pm |
|
 |
|
Parrothead
Commander
Joined: Wed May 03, 2006 2:00 am Posts: 1722 Location: USA
|
Well as for Bubble Sort at least the 2 or 3 ways I tried it in comparing 2 arrays allowed me to make lunch while I was waiting so instead of this little contest maybe posting some actual code that can be timed would be more useful.
As for Radix sorts what would the LSD be in the amount of port product be? Only useful in telling if a port has over 2000 or 3000 or 10000 product to sell or buy otherwise will have to be done over and over for each digit. You can tell True or false from it but an array would have to be loaded first requiring many DB calls so I dont see the time saving their either.
|
| Thu Apr 12, 2007 3:30 pm |
|
 |
|
LoneStar
Commander
Joined: Fri Jun 09, 2006 2:00 am Posts: 1401 Location: Canada
|
Parrothead wrote: Well as for Bubble Sort at least the 2 or 3 ways I tried it in comparing 2 arrays allowed me to make lunch while I was waiting so instead of this little contest maybe posting some actual code that can be timed would be more useful. As for Radix sorts what would the LSD be in the amount of port product be? Only useful in telling if a port has over 2000 or 3000 or 10000 product to sell or buy otherwise will have to be done over and over for each digit. You can tell True or false from it but an array would have to be loaded first requiring many DB calls so I dont see the time saving their either.
okay. I'm way tired. here's what I whipped up:
Code: setArray $msl 10 setVar $msl[1] 10 setVar $msl[2] 9 setVar $msl[3] 8 setVar $msl[4] 7 setVar $msl[5] 6 setVar $msl[6] 5 setVar $msl[7] 4 setVar $msl[8] 3 setVar $msl[9] 2 setVar $msl[10] 1 setVar $ii 1 while ($ii <= 10) setvar $i 1 setVar $temp $msl[1] setVar $hold 0 while ($i <= (10 - 1)) if ($temp > $msl[($i + 1)]) setVar $hold $msl[($i + 1)] setVar $msl[($i + 1)] $temp setVar $msl[$i] $hold end add $i 1 gosub :spamn end add $ii 1 end halt :spamn setVar $c 1 echo "*-------------------" echo "*temp " $temp " hold " $hold " i " $i " ii " $ii while ($c <= 10) echo "*"&ANSI_15&$msl[$c] add $c 1 end return
it's over simplified because 1: I gotta hit the sack, ,and 2: I gotta hit the sack.. hehe
_________________ ---------------------------- -= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
|
| Thu Apr 12, 2007 3:55 pm |
|
 |
|
Parrothead
Commander
Joined: Wed May 03, 2006 2:00 am Posts: 1722 Location: USA
|
so if this were 2 sector sized array of 20 k the 8000000000000 comparisions would be made? Guess im missing something.
|
| Thu Apr 12, 2007 4:56 pm |
|
 |
|
LoneStar
Commander
Joined: Fri Jun 09, 2006 2:00 am Posts: 1401 Location: Canada
|
Parrothead wrote: so if this were 2 sector sized array of 20 k the 8000000000000 comparisions would be made? Guess im missing something.
Yeah. Sigh. You appear to be. Your inital 'example' was loading a text file of MSL sectors via ReadAll , and THEN populated an Array sizeOf SECTORS ... holy moly Bat-Man.. take another look.. ReadAll your MSL list, and sort *that* Array.. you wouldn't ever need an array SizeOf SECTORS --to order your MSL list, which would max out at ho wmany Rows?? 20k?? Never saw that before
_________________ ---------------------------- -= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time.
|
| Thu Apr 12, 2007 8:41 pm |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
I'd sort a slew of sectors like this: Code: # $list[] is the array to sort setArray $sorted SECTORS setVar $i 1 while ($i <= $list) setVar $sorted[$list[$i]] 1 add $i 1 end setVar $i 1 setVar $sorted 0 while ($i <= SECTORS) if ($sorted[$i] = 1) add $sorted 1 setVar $sorted[$sorted] $i end add $i 1 end # you now have a sorted array of size $sorted # true, the static array is larger than $sorted, # but ignore everything higher than $sorted
I'm not a student of algorithms... I just make them up when I need them.
Sing, that was a cool recursive stack routine... I should post it.
+EP+
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Thu Apr 12, 2007 9:48 pm |
|
 |
|
Slim Shady
Gameop
Joined: Thu Jun 06, 2002 2:00 am Posts: 2371 Location: USA
|
I tawt I taw an EPcat!
_________________ Ask Slim!
--==[The Outfit]==--
|
| Thu Apr 12, 2007 9:55 pm |
|
 |
|
Singularity
Veteran Op
Joined: Thu Jun 02, 2005 2:00 am Posts: 5558 Location: USA
|
Hm... I thought AND/XOR/OR could do more complex bitwise ops, guess not. Delphi's can. That's very wierd. Bitwise would be faster than cutText for sure, but I'm not convinced that cutText would be slower than using a comparable bubble sort. LS, all arrays are treated as strings, I believe, in TWXproxy. It's certainly not retyping the variable every single time you change something.
For sorting sector numbers yea, you can just use a basic counting sort like ya'll are doing. But what if you want to sort by something more complex, by say distance from dock or traffic count? The former has a small spread, the latter... well my version gets very big very quickly.
Be nice if we could get the bitops to work more naturally.
And yea it was a cool recursion.
_________________ May the unholy fires of corbomite ignite deep within the depths of your soul...
1. TWGS server @ twgs.navhaz.com 2. The NavHaz Junction - Tradewars 2002 Scripts, Resources and Downloads 3. Open IRC chat @ irc.freenode.net:6667 #twchan 4. Parrothead wrote: Jesus wouldn't Subspace Crawl.
*** SG memorial donations via paypal to: dpocky68@booinc.com
|
| Thu Apr 12, 2007 10:05 pm |
|
 |
|
Parrothead
Commander
Joined: Wed May 03, 2006 2:00 am Posts: 1722 Location: USA
|
LoneStar wrote: Parrothead wrote: so if this were 2 sector sized array of 20 k the 8000000000000 comparisions would be made? Guess im missing something. Yeah. Sigh. You appear to be. Your inital 'example' was loading a text file of MSL sectors via ReadAll , and THEN populated an Array sizeOf SECTORS ... holy moly Bat-Man.. take another look.. ReadAll your MSL list, and sort *that* Array.. you wouldn't ever need an array SizeOf SECTORS --to order your MSL list, which would max out at ho wmany Rows?? 20k?? Never saw that before
The size of the example has what to do with the math? Grow up a little please. This Forum is for Adult discussion.
|
| Thu Apr 12, 2007 10:45 pm |
|
 |
|
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1134 Location: Augusta, GA
|
Heh, thanks Slim. You know I can't stay away from a good nerdy conversation for long
Sing, spell out the nature of the data to be sorted in more detail. If traffic count is simply a number, and you have a maximum of 20k numbers that need to be sorted, it shouldn't be too time-consuming. If it's something else, elaborate and maybe we can put our heads together. You know how I enjoy logic puzzles
What's with the mood swings in here?
+EP+
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
| Thu Apr 12, 2007 11:41 pm |
|
 |
|
Who is online |
Users browsing this forum: No registered users and 14 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|