best way to: Store the units with highest custom value first in a unit array?

polo2005

Wana start playing LoL? http://tinyurl.com/369as27
Reaction score
97
I'm looking for a smart way to store the unit with highest custom value from a unit_Group with x units inside.
for example
unit 1 have the custom value 5
unit 2 have the custom value 3
unit 3 have the custom value 3
unit 4 have the custom value 2

and in my unit_variable
unit 1 should be = unit_variable[1]
unit 2 or unit 3 should be = unit_variable[2] or [3]
unit 4 should be = unit_variable[4]
 

polo2005

Wana start playing LoL? http://tinyurl.com/369as27
Reaction score
97
well this is kinda a one set unit_variable so the unit with the highest custom value at that point should come first.
 

Accname

2D-Graphics enthusiast
Reaction score
1,463
You could iterate through all units in the unit-group repeatedly looking for the unit with the lowest custom value, removing it from the unit group, and adding it to the end of the array. This is a simple way, there are better methods, but with GUI it would take alot of work to implement them, more work then i think would be worth it.

Do you need to run this process several times with low time spans in between? If not, go for my suggestion.
 

polo2005

Wana start playing LoL? http://tinyurl.com/369as27
Reaction score
97
that will prob work, but sounds like it will take alot of time to implant this, would it be possible to do this smothly in jass (not vjass, i got zero experience with vjass)? if so, mind giving me an example?
 

huanAk

New Member
Reaction score
4
easy, here is how it do in GUI trigger:

Code:
   temp_unit 
   temp_integer = 0
   for every loop integer A from 0 to (count units in unit_group-1) do multiple action
		for every unit in unit_group do multiple action
			if custom value of picked unit > temp_integer
				temp_integer = custom value of picked unit
				temp_unit = picked unit
		else do nothing	
		unit_array[loop integer A] = temp_unit	 // unit_array[0] is highest
                remove temp_unit from uni_group
 

polo2005

Wana start playing LoL? http://tinyurl.com/369as27
Reaction score
97
wouldn't this only store the unit with highest custom value correctly?
 

polo2005

Wana start playing LoL? http://tinyurl.com/369as27
Reaction score
97
well nor do i :p so wouldn't know, but its for a req on a system for my brother so i pray it will work for him :) thanks alot
 

huanAk

New Member
Reaction score
4
wouldn't this only store the unit with highest custom value correctly?

did you try ?
- it sorted every unit in your unit group,
- and store them in array, order by custom value (from highest to lowest)

It is GUI trigger which only take a few click.

I don't understand why you need to
- download 2 page of code from other people and
- require NewGen
- require average or higher JASS knowledge to modified
- and it may not work in future patch (eg so many people use these S2I function and it not longer work)
 

Dirac

22710180
Reaction score
147
Because your answer is extremely slow, what you're doing there is called a bubble sort and it's exponentially slower for every unit inside the group. I use merge sort which is quite fast. There are other methods that are faster, but mine works for sorting array variables inside structs very well
 

huanAk

New Member
Reaction score
4
Because your answer is extremely slow, what you're doing there is called a bubble sort and it's exponentially slower for every unit inside the group. I use merge sort which is quite fast. There are other methods that are faster, but mine works for sorting array variables inside structs very well

Wrong,
"bubble sort" is compare every adjacent pair (Best case= N, worse case= N^2
mine is "selection sort" which pick highest unit in every turn (always N^2)

merge sort may have better average case ( N*logN ). but it may slower because :
- math compare is so fast, so N^2 compare won't take many CPU "operation"
- extra function, variablle, operation in Merg Sort mean it end up more CPU operation that just basic "selection sort"

remember, you are not soring million of unit, so the simplest "selection sort" = fastest sort
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
you are not soring million of unit, so the simplest "selection sort" = fastest sort

You can't definiteavely say that, any thread on here once answered is kept for later use with the search function and how many units are being sorted and stores differs from map to map. My map stores every single unit's custom data and has to frequently sort large groups, the merge sort method is much faster overall if the group is any larger than 10 units and at that small of a group your method's speed won't create any difference noticeable to anyone playing on a system more advanced than win98 with a shitty processor. The merge sort method takes the same amount of time for any ammount of units, the only increase in time would be if they want every single unit's position (in line) saved and even then I can think of a way to do it with the smallest possible ammount of functions.
 

SerraAvenger

Cuz I can
Reaction score
234
Wrong,
"bubble sort" is compare every adjacent pair (Best case= N, worse case= N^2
mine is "selection sort" which pick highest unit in every turn (always N^2)

merge sort may have better average case ( N*logN ). but it may slower because :

Just for the record:
bubblesort and selection sort are ALWAYS n*(n-1)/2 [EDIT:Bubblesort can be n, please read #19 and #20]
mergesort is ALWAYS n log n

Which means that for more than 3 units, mergesort will be faster.
Let us say that because of the additional stack overhead, mergesort has a constant of 10 as opposed to a constant of 1 in selection sort.
Then after 11 units, mergesort is faster.

Back to your problem: if you only need the unit with the lowest custom value, use a heap. if values are changed constantly, use a self-balancing search tree.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Just for the record:
bubblesort and selection sort are ALWAYS n*(n-1)/2

Bubblesort has a best case scenario of O(n), which is when the input is already sorted. It only goes through the list if it swapped any values in the first pass. This "n*(n-1)/2" is exactly the same as saying O(n^2), because constant values are negligible given that the input is 'big enough'.

mergesort is ALWAYS n log n

No one said it wasn't. :p Your point after this is still valid, however.

huanAk said:
merge sort may have better average case ( N*logN ). but it may slower because :
- math compare is so fast, so N^2 compare won't take many CPU "operation"
- extra function, variablle, operation in Merg Sort mean it end up more CPU operation that just basic "selection sort"

This is only true if the input is sufficiently small, and/or partially sorted already (iff the algorithm has a better best-case scenario), which are rare cases.
 

SerraAvenger

Cuz I can
Reaction score
234
Bubblesort has a best case scenario of O(n), which is when the input is already sorted. It only goes through the list if it swapped any values in the first pass.
You're absolutely right. I looked up bubblesort and found out that it uses a clever condition to exit early when the array was sorted. The sorting algorithm I had in mind didn't do that (and hence was in Theta(n^2)). Not that it mattered much though ; )

This "n*(n-1)/2" is exactly the same as saying O(n^2), because constant values are negligible given that the input is 'big enough'.
Jup.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top