• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Integer Array ordering by value

Status
Not open for further replies.
Level 17
Joined
Nov 13, 2006
Messages
1,814
example:

int[1]=3
int[2]=2
int[3]=1
int[4]=4

how can i order this to desc?

i tryed make a loop

like this

we got a index lets call it "index", we compare this with other index

for a = 1 to max array index
if index > a
i=int[a]
int[a]=int[index]
int[index]=i
endif
endloop

this work well until difference between a and index is 1, but example if a = 1 and index = 4 and its swap each other then even a maybe higher than 2 and 3 but its get the 4th position

(i want make this in gui)

sorry for crap english :D
 
Level 4
Joined
Aug 5, 2011
Messages
99
ok, heres probably the least efficient algoritm i can think of, but maths in gui..... well, that takes a while
  • sort
    • Events
      • Time - Elapsed game time is 2.00 seconds
    • Conditions
    • Actions
      • For each (Integer A) from 1 to arrayLength, do (Actions)
        • Loop - Actions
          • Set string = ((string + ) + (String(array[(Integer A)])))
      • Game - Display to (All players) for 30.00 seconds the text: (before sort: + string)
      • For each (Integer A) from 1 to arrayLength, do (Actions)
        • Loop - Actions
          • For each (Integer B) from 2 to arrayLength, do (Actions)
            • Loop - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • array[(Integer B)] Greater than array[((Integer B) - 1)]
                • Then - Actions
                  • Set temp = array[(Integer B)]
                  • Set array[(Integer B)] = array[((Integer B) - 1)]
                  • Set array[((Integer B) - 1)] = temp
                • Else - Actions
      • Set string =
      • For each (Integer A) from 1 to arrayLength, do (Actions)
        • Loop - Actions
          • Set string = ((string + ) + (String(array[(Integer A)])))
      • Game - Display to (All players) for 30.00 seconds the text: (after sort: + string)
 

Attachments

  • test.w3x
    16.7 KB · Views: 53
Level 14
Joined
Apr 20, 2009
Messages
1,543
  • sort
    • Events
    • Conditions
    • Actions
      • For each (Integer A) from 1 to MaxArray, do (Actions)
        • Loop - Actions
          • For each (Integer B) from 1 to MaxArray do (Actions)
            • Loop - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • And (All Conditions are True)
                    • Conditions
                      • [Integer A] not equal to [Integer B]
                      • [Integer B] Greater than [Integer A]
                      • int[Integer A] Greater than int[Integer B]
                • Then - Actions
                  • Set TempInt = int[Integer A]
                  • Set int[Integer A] = int[Integer B]
                  • Set int[Integer B] = TempInt
Would something like this suffice?

example:
int[1] = 12
int[2] = 3
int[3] = 5
int[4] = 14

if int[1] Greater than int[2]
int[1] = 3
int[2] = 12

int[1] is not greater than any other int so now Integer A is int[2]
int[2] Greater than int[3]
int[2] = 5
int[3] = 12

Order:
int[1] = 3
int[2] = 5
int[3] = 12
int[4] = 14

EDIT: I haven't tested it yet but I assume in theory it works.
Here's another example which should work:

int[1] = 9
int[2] = 3
int[3] = 8
int[4] = 6

int[1] Greater than int[2]
int[1] = 3
int[2] = 9
int[3] = 8
int[4] = 6

int[2] Greater than int[3]
int[1] = 3
int[2] = 8
int[3] = 9
int[4] = 6

int[2] Greater than int[4]
int[1] = 3
int[2] = 6
int[3] = 9
int[4] = 8

int[3] Greater than int[4]
int[1] = 3
int[2] = 6
int[3] = 8
int[4] = 9
 
Last edited:
Level 4
Joined
Aug 5, 2011
Messages
99
no that doesnt work hash, you shouldnt be comparing values from integer a with values from integer b... dont mix things up, u need a double loop just to repeat the progress... if u dont believe me, check it in the file i added... it just does random stuff

basic algoritms for example find the lowest value out of the array, put it in front.. then start looking from 2nd index and find the lowest, this repeated (arraylength) times

or the one i posted (which i tested) just compares #1 with #2 and if second is higher than first, they get swapped.. then compare #2 with #3, #3 with #4 and so on.. when u get at the end, you just start over (arraylength) times.
 

Attachments

  • test(3).w3x
    16.8 KB · Views: 65
Level 14
Joined
Apr 20, 2009
Messages
1,543
no that doesnt work hash, you shouldnt be comparing values from integer a with values from integer b... dont mix things up, u need a double loop just to repeat the progress... if u dont believe me, check it in the file i added... it just does random stuff

basic algoritms for example find the lowest value out of the array, put it in front.. then start looking from 2nd index and find the lowest, this repeated (arraylength) times

or the one i posted (which i tested) just compares #1 with #2 and if second is higher than first, they get swapped.. then compare #2 with #3, #3 with #4 and so on.. when u get at the end, you just start over (arraylength) times.

Erm look again, that's exactly what my algorithm does.

EDIT: I'll re-do it later, got to get back home. I'm still at school...
I've done this algorithm before, so I might have done something wrong this time, I'll fix it soon.

EDIT2: nevermind, it seems that it does work...
 
Last edited:
Level 4
Joined
Aug 5, 2011
Messages
99
[Integer B] Greater than [Integer A] doesnt make sense as condition, make the inner loop go from integer A to (arraylength)
[Integer A] not equal to [Integer B] is excluded by the above condition

Set TempInt = int[Integer A]
Set int[Integer A] = int[Integer B]
Set int[Integer B] = TempInt
this part doesnt make sense at all, replace integer A with ( integer B + 1 )
 
Level 14
Joined
Apr 20, 2009
Messages
1,543
[Integer B] Greater than [Integer A] doesnt make sense as condition, make the inner loop go from integer A to (arraylength)
[Integer A] not equal to [Integer B] is excluded by the above condition

Set TempInt = int[Integer A]
Set int[Integer A] = int[Integer B]
Set int[Integer B] = TempInt
this part doesnt make sense at all, replace integer A with ( integer B + 1 )


Hmmm.... Then why does it work ;)?

My trigger works, you just can't comprehend it :)
Would you like me to explain?

EDIT:
[Integer A] not equal to [Integer B] is excluded by the above condition
You are right about this part, but I like to be safe just in case.

[Integer B] Greater than [Integer A] doesnt make sense as condition, make the inner loop go from integer A to (arraylength)
If [Integer B] would be less than [Integer A] then it will re-do the entire process and shuffle the numbers back to it's original place, which is what we don't want.

For example if Integer A = 2 and Integer B = 1 then you don't want to check if int[Integer A] is greater than int[Integer B] since it then switches the values back to it's original place.

Set TempInt = int[Integer A]
Set int[Integer A] = int[Integer B]
Set int[Integer B] = TempInt

TempInt will store the value of int[Integer A] so that it doesn't get lost.
Then int[Integer A] will be the value of int[Integer B] and int[Integer B] will have the previous value of int[Integer A]
It basically switches the numbers whenever int[Integer A] is greater than int[Integer B].
 

Attachments

  • Sort.w3x
    16.6 KB · Views: 41
Last edited:
Level 4
Joined
Aug 5, 2011
Messages
99
i feel stupid now >.< shud think before i open my mouth lol

If [Integer B] would be less than [Integer A] then it will re-do the entire process and shuffle the numbers back to it's original place, which is what we don't want.
i know, but u can down on the comparisons to make it a tiny bit faster :) this way the loop just skips the values instead of checking if it should execute

  • For each (Integer A) from 1 to MaxArray, do (Actions)
    • Loop - Actions
      • For each (Integer B) from ((Integer A) + 1) to MaxArray, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • int[(Integer A)] Greater than int[(Integer B)]
            • Then - Actions
              • Set tempInt = int[(Integer A)]
              • Set int[(Integer A)] = int[(Integer B)]
              • Set int[(Integer B)] = tempInt
            • Else - Actions
btw, the reason i thought it didnt work was cuz i made a lil mistake copying the thingy u wrote earlier :/
 
Level 14
Joined
Apr 20, 2009
Messages
1,543
i feel stupid now >.< shud think before i open my mouth lol


i know, but u can down on the comparisons to make it a tiny bit faster :) this way the loop just skips the values instead of checking if it should execute

  • For each (Integer A) from 1 to MaxArray, do (Actions)
    • Loop - Actions
      • For each (Integer B) from ((Integer A) + 1) to MaxArray, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • int[(Integer A)] Greater than int[(Integer B)]
            • Then - Actions
              • Set tempInt = int[(Integer A)]
              • Set int[(Integer A)] = int[(Integer B)]
              • Set int[(Integer B)] = tempInt
            • Else - Actions
btw, the reason i thought it didnt work was cuz i made a lil mistake copying the thingy u wrote earlier :/

Don't worry man, everyone makes mistakes. It makes us human :)

i know, but u can down on the comparisons to make it a tiny bit faster :) this way the loop just skips the values instead of checking if it should execute
The execution time is near nihil. But if you are a optimization freak who cares about microseconds, then yes it does make it a microscopic amount faster :p
And I don't consider being a optimization freak is a bad characteristic. On the contraire I personally like optimalization a lot. It's good that you pointed this out.
 

sentrywiz

S

sentrywiz

You want to sort an array in desc?

The standard algo for finding the greatest number, only store it in a new array. Change the found number to 0 and move the original into the new array as first. Continue doing it for as long as original array has numbers in it.
 
Level 14
Joined
Apr 20, 2009
Messages
1,543
You want to sort an array in desc?

The standard algo for finding the greatest number, only store it in a new array. Change the found number to 0 and move the original into the new array as first. Continue doing it for as long as original array has numbers in it.

Why use 2 arrays if you can sort the array containing the numbers as shown in my posts? Usage of excessive arrays in my opinion is unnessecary since it takes up space.
 
Level 17
Joined
Nov 13, 2006
Messages
1,814
hehe i see have a nice discusion but situation in work i got a ideea and its work well with single loop

TempNr = last updated index in linked list

  • For each (Integer a) from 1 to (TempNr - 1), do (Actions)
    • Loop - Actions
      • Set b = (TempNr - a)
      • Set c = (b + 1)
      • //here a longer if block what set boolean variable to false or true
        • If - Conditions
          • Boolean Equal to True
        • Then - Actions
          • Set i = Mb_TotalKill[c]
          • Set Mb_TotalKill[c] = Mb_TotalKill[b]
          • Set Mb_TotalKill[b] = i
          • Set i = Mb_HeroKill[c]
          • Set Mb_HeroKill[c] = Mb_HeroKill[b]
          • Set Mb_HeroKill[b] = i
          • Set i = Mb_CreepKill[c]
          • Set Mb_CreepKill[c] = Mb_CreepKill[b]
          • Set Mb_CreepKill[b] = i
          • Set i = Mb_Player[c]
          • Set Mb_Player[c] = Mb_Player[b]
          • Set Mb_Player[b] = i
          • Set Mb_Player_Index[Mb_Player[c]] = c
          • Set Mb_Player_Index[Mb_Player[b]] = b
        • Else - Actions
 
Level 17
Joined
Nov 13, 2006
Messages
1,814
i made abc only for shorter code so more visible everything, anyway i love the linked list since i learned, coz of this ,,Mb_Player_Index'' when i can check unit linked list index easily and manipulate it, just image it the projectile system funny too with this, each dummy projectile unit have 1 index in linked list what u can check and lets say u want do a buff what pick every missile unit in 1000 range and slow them down :p

in this example its only used for each player get his color in multiboard even his index is changed :)
 
Level 14
Joined
Apr 20, 2009
Messages
1,543
i made abc only for shorter code so more visible everything, anyway i love the linked list since i learned, coz of this ,,Mb_Player_Index'' when i can check unit linked list index easily and manipulate it, just image it the projectile system funny too with this, each dummy projectile unit have 1 index in linked list what u can check and lets say u want do a buff what pick every missile unit in 1000 range and slow them down :p

in this example its only used for each player get his color in multiboard even his index is changed :)

This has potential :D!
 
Status
Not open for further replies.
Top