calc median g_

This commit is contained in:
olli 2024-08-16 23:17:33 +02:00
parent 4174b3f7e3
commit be7e5da172

View File

@ -11,18 +11,17 @@ function g_median {
local g_num_elements=${#g_sorted_numbers[@]} local g_num_elements=${#g_sorted_numbers[@]}
# calculate the middle # calculate the middle
local g_middle=$((g_num_elements/2)) local g_middle=$(($g_num_elements/2))
local g_median local g_median
# even/odd number # even/odd number
if ((g_num_elements % 2 == 1)) if (($g_num_elements % 2 == 1))
then then
# odd number # odd number
echo "g_middle=$g_middle" g_median=${g_sorted_numbers[$g_middle]}
g_median=${g_sorted_numbers[g_middle]}
else else
# even number # even number
g_calc "(${g_sorted_numbers[g_middle - 1]} + ${g_sorted_numbers[g_middle]}) / 2" g_calc "(${g_sorted_numbers[$g_middle - 1]} + ${g_sorted_numbers[$g_middle]}) / 2"
g_median=$g_calc_result g_median=$g_calc_result
fi fi