| 01-20-2006, 09:02 PM | #1 |
Is there a way to order a bunch of integer values from greatest to least or least to greatest? well, may not have to be integers, but basically what i want to do is take the player with the highest lvl hero, and assign him to team 1. 2nd highest assign to team 2, 3rd to team 1,.. This is basically to make fair teams for a arena part of my game. |
| 01-20-2006, 09:06 PM | #2 |
Well... you could loop by all players to get highest assign him to team 2, loop by all players for almost highest and so, but this will be highly uneffective I think. |
| 01-20-2006, 09:16 PM | #3 |
You can do a quick sort. I can give you a QB 4.5 version quicksort and you can try to change it to JASS. It's slightly obfuscated so you'll (or some other hapless soul maybe..) need to make the GOTOs into functions.. Code:
SUB qsort.linked.integer.highstart (array1() AS INTEGER, array2() AS INTEGER, a.max%) DIM g2(1 TO 32) AS INTEGER, h2(1 TO 32) AS INTEGER,i AS INTEGER, _ j AS INTEGER, r AS INTEGER, E AS INTEGER, g AS INTEGER, h AS INTEGER, k AS INTEGER E = 1: g2(1) = 1: h2(1) = a.max% e1: g = g2(E): h = h2(E) e2: i = g: j = h: r = (g + h) \ 2: k = array1(r) e3: IF array1(i) > k THEN i = i + 1: GOTO e3 e4: IF array1(j) < k THEN j = j - 1: GOTO e4 IF i <= j THEN SWAP array1(i), array1(j): SWAP array2(i), array2 _ (j): i = i + 1: j = j - 1: IF i <= j THEN GOTO e3 IF j - g + i < h THEN IF i < h THEN g2(E) = i: h2(E) = h: E = E + 1 h = j ELSE IF g < j THEN g2(E) = g: h2(E) = j: E = E + 1 g = i END IF IF g < h THEN GOTO e2 ELSE E = E - 1: IF E THEN GOTO e1 ERASE g2, h2 END SUB |
| 01-20-2006, 10:31 PM | #4 |
quicksort won't work cause you can't have array arguments in functions. To use it you would need gamecache and remove the Quick preffix Use bubble sort, they are just 12 players anyways Note that you have to sort the players too. you would need to arrays or something like that. A[] holds the values you want to sort and B[] the numbers of the players. I am gonna use pseudo code pseudocode:for (i=1 to 11) { for (j=i+1 to 12) if (A[j]>A[i]) { x=A[j] A[j]=A[i] A[i]=x //Also swap elements in B x=B[j] B[j]=B[i] B[i]=x } } It is NOT the best sorting algorythm but It is - at least for me - the easiest to understand and it is good enough when there are just 12 elements. The result is that A and B have the elements sorted out in Descendant order, if you want ascendant order use < instead of > |
| 01-20-2006, 10:46 PM | #5 |
The quick sort I posted is iterative. :-D |
| 01-20-2006, 10:58 PM | #6 |
um... i dont really understand this. I almost understand what Vex posted.. but this is too complicated for me.. But thanks for the quick reply. I used what i understand from what u 2 posted to make a less complex.. but not as functional sorting system. I tested it a few times, and it makes the teams fair enough for what i need. Thanks guys :) |
| 01-20-2006, 11:05 PM | #7 | |
Quote:
|
| 01-20-2006, 11:21 PM | #8 |
"GOTO" type stuff seems like it would be hard to replicate in a language like JASS. |
| 01-21-2006, 02:15 PM | #9 |
All you need to do is make everything a function , but GOTO is better than that in this case, in terms of pure complexity.. It would be a mess in JASS though.. as everything. |
| 01-21-2006, 02:18 PM | #10 |
GOTOs are harmful according to dijkvistra. And well JASS doesn't have structures like while , for or repeat until it uses exitwhen that can act the same as gotos |
| 01-21-2006, 03:04 PM | #11 |
Everything is just a masked set of GOTOs... There's also an article called ""GOTO Considered Harmful" Considered Harmful": Frank Rubin, Communications of the ACM, 30(3):195-196, March 1987. He says, quote: 'I have yet to see a single study that supported the supposition that GOTOs are harmful (I presume this is not because nobody has tried). Nonetheless, people seem to need to believe that avoiding GOTOs will automatically make programs cheap and reliable. They will accept any statement affirming that belief, and dismiss any statement opposing it. "It has gone so far that some people have devised program complexity metrics penalizing GOTOs so heavily that any program with a GOT0 is ipso facto rated more complex than even the clumsiest GOTO-less program. Then they turn around and say, “See, the program with GOTOs is more complex.” In short, the belief that GOTOs are harmful appears to have become a religious doctrine, unassailable by evidence.' |
| 01-21-2006, 05:01 PM | #12 |
But in this case your code it pointless, he's only sorting several elements, so a simple bubble sort is really all you need.... |
| 01-21-2006, 06:19 PM | #13 |
GOTOs should only be used when programming at lower levels. And not using GOTOs doesn't make your code more reliable. But using them simply breaks logic and increase the chances of making the program less reliable |
| 01-21-2006, 07:13 PM | #14 |
That's only true if you don't need to use them... F. Rubin's example following that quote is this: He got 16 programmers to each make him a program that returns the first row, if, in an NxN matrix of 0's and 1's, there is a row that has all 0's. This was in 1987. Three used GOTOs to get out of the nested loop and ten used flags. The ones that used flags were much more complex than the ones that used GOTOs. Example program, with GOTO, in basic: Code:
for i = 1 to n for j = 1 to n if matrix(i, j) <> 0 then goto has_a_one next n print n exit for has_a_one: next n The best non-GOTO version I could come up with: Code:
for i = 1 to n for j = 1 to n if matrix(i, j) <> 0 then j = 0: exit for next n print n if j <> 0 then print n: exit for next n Arguably just a little bit more complicated than GOTO, but they didn't have "exit for" back then.. and "exit for" is just a GOTO loop too.. Also this one would require j to not use one number (in this case, 0), or require a new variable (flag) altogether... IMO goto is useful but only for people who won't abuse it... |
| 01-21-2006, 07:27 PM | #15 |
I don't see why you use code tags since there is no indent at all in that code. I don't understand that language you use, I come from a world of whiles fors and repeat untils. Where there is no thing like next and those words. but I think I understood the question so I am gona solve it without gotos Code:
i=j=1;
while ((i<=n) && (j<=n))
{
if (M[i][j]!=0)
{
j=1;
i++;
}
else if (j==n)
{
Print(i);
n=0;
}
else
j++;
}For the record while uses a jump internally. But it kind of encapsullates it. If you use goto to simulate a while statement the logic is still right. The problem with gotos is not using them but using them with too much liberty causing flawed logic Edit: this one seems cleaner: Code:
i=j=1;
while ((j<=n) && (i<=n))
{
if (M[i][j]!=0) { j=0; i++; }
j++;
}
Print(i) //returns a number bigger than n when there is no row like thatEdit II: maybe by that time the only alternative to gotos were fors like the ones you used. In that case I would be a GOTO worshiper too. |
