Efficiently calculate every enemy's remaining-HP:MHP ratio and target the lowest one for a skill?

I have a skill called "Natural Selection" that automatically targets the enemy who has taken the most damage proportionate to their health bar. For example, say I had an enemy with 10,000/40,000HP (25%) remaining and another with 2,000/2,500HP (80%) remaining in the same troop when the player uses this skill.

How do I set up a-- I presume a common event triggered by casting the skill? --common event/damage formula that accomplishes this in a manner that's efficient to event?
I can see many ways to do this.
  1. Using a troop event to force an action at the end of each turn.
    If you do it this way, you have to use a script call and store the target into a variable

  2. Targeting all enemies at once and calculating the right target using the damage formula.
    When doing it this way, you have to keep in mind that, as any other skill that relies on the damage formula, the formula is only applied when the skill hits. Be sure to set it in the right way.
That said, when we talk about efficiency, we usually consider efficiency related to the time spent to perform a certain action. However, it is also possible to consider efficiency based on the time spent to write the code itself. In this case, there is no big difference in terms of time spent coding the action itself, however, there is a big difference when it comes to how the action is performed and how much the engine need to perform that action.

The code to store your target is always the same:
Code:
$game_variables[var_id_for_hp_rate] = 1
$game_variables[var_id_for_target] = 0
$game_switches[switch_id_for_flag] = false
$game_troop.members.each.with_index do |e, i|
  next unless e.alive?
  if (e.hp_rate < $game_variables[var_id_for_hp_rate])
    $game_variables[var_id_for_hp_rate]
    $game_variables[var_id_for_target] = i
    $game_switches[switch_id_for_flag] = true
  end
end

This allows you to force an action based on the target id (stored in $game_variables[var_id_for_target]), to access the target using $game_troop.members[$game_variables[var_id_for_target]] and only cast the skill if a valid target was found ($game_switches[switch_id_for_flag] is true). When the switch is false, enemies are dead or they did not take damage at all. If the former is true, unless you are using a script to prevent that, BattleManager.judge_win_loss returns true and the battle ends (so the event is not going to be executed and you can discard this option at all, it is not worth considering), if the latter is true, you can simply force the action against a random target.
Just to simplify things. The below code will give you the enemy index with the lowest HP.
Code:
$game_troop.alive_members.min_by {|e| e.hp_rate}.index
The below code will give you the enemy index with the lowest HP.
@FirestormNeos just be aware of the fact that it is not the most "efficient" way to do it if you put it in a damage formula that targets all enemies. Not if you mean "efficient" as in "efficient algorithm". However, it definitely is the most efficient if you mean "efficient" as "the fastest solution to write".
I meant efficient as in "fastest solution to write" when I wrote the but I appreciate the information, @Heirukichi. I learned something new today about algorithm efficiency! 😀

Original thread: https://forums.rpgmakerweb.com/threads/efficiently-calculate-every-enemys-remaining-hp-mhp-ratio-and-target-the-lowest-one-for-a-skill.112756/