calc median g_

This commit is contained in:
olli 2024-08-16 23:35:21 +02:00
parent 4342f0a5f8
commit c761fe5d93

View File

@ -13,18 +13,14 @@ function g_median {
# calculate the middle # calculate the middle
local g_middle=$(($g_num_elements/2)) local g_middle=$(($g_num_elements/2))
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
g_median="${g_sorted_numbers[$g_middle]}" g_median_result="${g_sorted_numbers[$g_middle]}"
echo "$g_median - ${g_sorted_numbers[$g_middle]} - $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_result="$g_calc_result"
fi fi
g_median_result=$g_median
} }