How to resolve the algorithm Averages/Median step by step in the Bracmat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Median step by step in the Bracmat programming language

Table of Contents

Problem Statement

Write a program to find the   median   value of a vector of floating-point numbers. The program need not handle the case where the vector is empty, but must handle the case where there are an even number of elements.   In that case, return the average of the two middle values. There are several approaches to this.   One is to sort the elements, and then pick the element(s) in the middle. Sorting would take at least   O(n logn).   Another approach would be to build a priority queue from the elements, and then extract half of the elements to get to the middle element(s).   This would also take   O(n logn).   The best solution is to use the   selection algorithm   to find the median in   O(n)   time. Quickselect_algorithm

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Median step by step in the Bracmat programming language

Source code in the bracmat programming language

( ( median
  =     begin decimals end int list
      , med med1 med2 num number
    .   ( convertToRational
        =
          .     new$(UFP,'(.$arg:?V))
              : ?ufp
            & (ufp..go)$
            & (ufp..export)$(Q.V)
        )
      & 0:?list
      &   whl
        ' ( !arg:%?number ?arg
          & convertToRational$!number:?rationalnumber
          & (!rationalnumber.)+!list:?list
          )
      & !list:?+[?end
      & (   !end*1/2:~/
          &   !list
            :   ?
              + [!(=1/2*!end+-1)
              + (?med1.?)
              + (?med2.?)
              + ?
          & !med1*1/2+!med2*1/2:?med
        | !list:?+[(div$(1/2*!end,1))+(?med.)+?
        )
      & (new$(UFP,'(.$med)).go)$
  )
& out$(median$("4.1" 4 "1.2" "6.235" "7868.33"))
&   out
  $ ( median
    $ ( "4.4"
        "2.3"
        "-1.7"
        "7.5"
        "6.6"
        "0.0"
        "1.9"
        "8.2"
        "9.3"
        "4.5"
      )
    )
& out$(median$(1 5 3 2 4))
& out$(median$(1 5 3 6 4 2))
);

  

You may also check:How to resolve the algorithm Special characters step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the Wren programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Frink programming language