How to resolve the algorithm Averages/Median step by step in the Phixmonti programming language
How to resolve the algorithm Averages/Median step by step in the Phixmonti 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 Phixmonti programming language
Source code in the phixmonti programming language
include ..\Utilitys.pmt
def median /# l -- n #/
sort len 2 / >ps
tps .5 + int 2 slice nip
ps> dup int != if
1 get nip
else
sum 2 /
endif
enddef
( 4.1 5.6 7.2 1.7 9.3 4.4 3.2 ) median ?
( 4.1 7.2 1.7 9.3 4.4 3.2 ) median ?
You may also check:How to resolve the algorithm N'th step by step in the Pascal programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Nim game step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Seed7 programming language
You may also check:How to resolve the algorithm File extension is in extensions list step by step in the Ada programming language