| 04-25-2006, 02:45 PM | #1 |
I have read a lot of posts and finally I also found one which described the same problem that I have at the moment. The only thing with it was, that noone answered or even replied to this thread . So here is the link to the prob I have right now:http://www.wc3campaigns.net/showthre...ort+multiboard Would be good if someone has an idea... |
| 04-25-2006, 03:09 PM | #2 |
You really don't need to worry about the speed of your sorting algorithm with only 12 players. Unless you're worried about stability, the easiest way is to do a simple selection sort on the player array. You can also modify selection sort to be stable. Altering the multiboard does not generate (much) net traffic. The trigger is run on each player's individual computer, and the only net traffic is to make sure the game doesn't desync. Using a hidden leaderboard would work perfectly, and wouldn't require you to write any sorting algorithm. |
| 04-25-2006, 04:53 PM | #3 |
insertion sort is generally speaking a very easy algorithm, and with such a small list of items the speed should be neglible compared to something like quicksort.It is also the best O(n^2) algorithm, with an avg case of n^2/4 and linear in bestcase. It's a stable sort and it can be done in place. assume udg_p[i] is an array of players, and udg_v[i] the values you want sorted, and udg_num is the number of players in the map. JASS:function insertionSort takes nothing returns nothing local int i=1 local int j local int v local player p loop exitwhen i==udg_num set v=udg_v[i] set p=udg_p[i] set j=i-1 loop exitwhen j<1 or udg_v[j]<=v set udg_v[j+1]=udg_v[j] set udg_p[j+1]=udg_p[j] set j=j+1 endloop set udg_v[j+1]=v set udg_p[j+1]=p set i=i+1 endloop endfunction |
| 04-25-2006, 06:19 PM | #4 |
Sorry for late review, but I tried to help in another thread... So back to Telamon and my problem... Code:
insertion sort is generally speaking a very easy algorithm, and with such a small list of items the speed should be neglible compared to something like quicksort @weaaddar So you think that the code you wrote in JASS is better then the use of a hidden leaderboard? And if yes: I'm not so far that I understand this code completely. The biggest problem I have is, how can you refer with it to the multiboard? Can you please give a short example? Because when it's faster then using a hidden leaderboard I would prefer to use this "insertion sort". Thanks so far Vuen & Weaaddar ! |
| 04-25-2006, 06:55 PM | #5 |
The multiboard in aftermath is bubbled. it works fine, 10-12 items is nothing. however, all the cool kids are using heap sort. you do want to be cool, don't you? JASS://sorted initialized to 0,1,2,3,4... //afterwards, sorted is ordered to index priority[] in decreasing order set nhs = n set i = nhs /2 loop //Transcribed from [url]http://en.wikipedia.org/wiki/Heapsort[/url] if(i > 0) then set i = i - 1 set t = sorted[i] else set nhs = nhs - 1 exitwhen nhs <= 0 set t = sorted[nhs] set sorted[nhs] = sorted[0] endif set parent = i set child = i+i+1 loop exitwhen child >= nhs if(child + 1 < nhs and priority[sorted[child+1]] < priority[sorted[child]]) then set child = child + 1 endif if(priority[sorted[child]] < priority[t]) then set sorted[parent] = sorted[child] set parent = child set child = parent+parent+1 else exitwhen true endif endloop set sorted[parent] = t endloop To use any of these sorts, keep all your data in arrays. Each time you feel like sorting , reset all the multiboard elements to the new values, rather than trying to inplace sort. |
| 04-25-2006, 10:12 PM | #6 |
Pipedream as much of an epenis it might give you to use heapsort, you have to realize that its actually worse then in insertion sort on lists of a dozen elements. (building the heap operation is nontrivial). Heapsort is an unstable but can be in place. Its one of the slower n*log(n) sorts, and will lose to insertion sort on a small lists (roughly speak sub 100 elements) since insertion sort on avg is n^2/4, (atleast hte implementation above), and building the heap a lone requires 2*n*log2(n), when n=12, we see that insertion sort absolutely wipes the floor with a time of only 36, but merely to build the heap requires ~87 operations! On small array you should never incur n*log(n) sorts unless your in a functional language with lists, in which case its probably easier to implement merge sort then even insertion sort. Stick with insertion sort. The only reason to use this method vs a multiboard is just that its in place and clean. All you have to do is call insertionSort() and then build a leaderboard with the values located at p[i] for the player and v[i] for the value. I don't remember how leaderboard work at all. I just did this totally from memory so my code may not compile. |
| 04-26-2006, 10:21 PM | #8 |
Those two lines do different things. Set assigns the value of the right expression to the left variable. For example, suppose A = 4 and B = 3. set A = B results in A = 3, B = 3 set B = A results in A = 4, B = 4 The real name of any global variable is udg_<variable editor name>. You use the real udg_name whenever you write custom script. Only use the variable editor name, without udg, when you are pulling something down from a GUI menu. Four more udg's should do it: Trigger: ![]() Custom script: set udg_LocalInteger_Kills=udg_LocalInteger_Kills[udg_LocalInteger]![]() Custom script: set udg_LocalPlayer=udg_LocalPlayer[udg_LocalInteger]I strongly recommend you bite the bullet and convert the trigger to custom text. |
| 04-27-2006, 03:23 PM | #9 |
you shouldn't use locals except for the ones declared in my function. Convert the trigger to custom text once your convinced the event is correct. Then copy and paste my code into your favorite texteditor and do word replaces for p to LocalPlayer and v to kills or whatever. assignment is as follows set variable=(expression). udg_v[i] is a variable, and an expression. Think of it like mathamatics, you say things like Let n=1, but then meddling through your proof you may actually assign some othing value in terms of n. Like a proof by induction on a recurrence relation: If A[n]=2*A[n-1] and A[1]=1 then A[n]=2^(n-1) Proof: Basis step: Let n=1, A[1]=1=2^(1-1) which is what we want. Inductive Step: Assume for some n, A[n]=2^(n-1), it will suffice to show for n+1 that A[n+1]=2^(n). A[n+1]=2*A[n] A[n+1]=2*(2^n-1) (By our inductive hypothesis) A[n+1] = 2^n QED. |
| 04-28-2006, 04:10 AM | #11 |
I'm a computer science student if it wasn't obvious. I also made a mistake. It should be j=j-1. whoops. Also don't use udg_ for your local variables. You should only use at most one udg_ local for your trigger. And it appears you don't need any for this trigger, anyway. It's much easier to read your code if your format it like I did, get rid of those long bulky var names. udg_IntLocalA is way too long and doesn't really tell you anything. i is much easier on the eyes. Anyway here is a very quick explanation as to what the algorithm does. You start at index 1 (arrays start at 0), and say is this element in the right spot? If it isn't shift every element up one until you find one element that is bigger then this. Once you find it, place the element in that index. Consider this example from a halfway sorted array 10 20 30 40 15 <- index at currently. --- 10 20 30 40<- next index 40 --- 10 20 30 <- index 30 40 --- 10 20<-- next index 20 30 40 --- 10<-- no need to go further 10 20 30 40 --- 10 15 <-- this is the correct spot 20 30 40 --- 10 15 20 <-- good spot 30 40 --- 10 15 20 30 <-- correct spot 40 --- 10 15 20 30 40 <-- correct spot --- Array is sorted. |
| 04-28-2006, 03:40 PM | #12 |
1) So where exactly lies the mistake?, is it in the 2nd loop where j=j+1, so if i'm right I have to use j=j-1 instead of j=j+1? EDIT: As I said, this replacement should be right (figured out by myself). 2) Only short question: When I'm in the 2nd loop for the first time and say that v[1] = v[0] and p[1] = p[0] what does it declare there?, I mean I have 8 player (p[1-8]) and so I have 8 values (v[1-8])! So what is p[0] and v[0]? If p[0] and v[0] were 0, then the whole algorithm would just set all values from p[1-8] and v[1-8] to 0, and there nothing is sorted or? Edit: Also figured out by myself (p[0] and v[0] will never come to use, because of the "exitwhen") |
| 04-29-2006, 08:17 PM | #13 |
Oh my god, I think I understood. The mistake I made was that I thought I had to sort in this way: 1----10 2----15 3----5 4----20 5----0 6----30 7----15 8----0 but with this sort algorith I have to sort in this way: 8----10 7----15 6----5 5----20 4----0 3----30 2----15 1----0 Yay! I think this was my problem, so now I have to see if I get it to work. I will inform you soon. |
| 04-29-2006, 09:10 PM | #14 |
Ah, I made out another thing I thought wrong: I was sure that when the command "exitwhen x<y" appears and its conditions are true, then the commands between this "exitwhen" and the "endloop" are still run for one last time, BUT that is not true! Ha! When the condition in the "exitwhen"-command is true, then the loop is immediately quitted. Man that was a hard nut Although, the command "exitwhen" in fact already say this Fine for now, but still no working multiboard with this algorithm. Write back when I'm further! |
| 04-29-2006, 10:06 PM | #15 |
I understand this algorithm quite good now and I have to ask: "Man! Who created this algorithm?! It is genius! Very good idea!" But still I have one problem. Is there perhaps still another mistake in the trigger? I mean "udg_num" (the variable set up for player range) should be number of players in the map + 1, or? If not then the player in the last index position will not be implemented or? |
