Skip to content

Add function to calculate the median of a set of numbers #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
delphidabbler opened this issue Jan 10, 2025 · 2 comments
Closed

Add function to calculate the median of a set of numbers #41

delphidabbler opened this issue Jan 10, 2025 · 2 comments
Assignees
Labels
completed Issue completed and committed to develop. To be closed on next release enhancement New feature or request

Comments

@delphidabbler
Copy link
Owner

There should be different implementations of Median for floating point and integer medians:

function Median(const A: array of Extended): Extended; overload;
begin
  Assert(Length(A) > 0);
  // optimisations for array lengths 1 & 2 to avoid sorting
  if Length(A) = 1 then
    Exit(A[0]);
  if Length(A) = 2 then
    Exit((A[0] + A[1]) / 2.0);
  Generics.Collections.TArray.Sort<Extended>(A); // using standard comparer
  var MiddleLo := Length(A) div 2;
  if Odd(Length(A)) then
    Result := A[MiddleLo + 1]
  else
    Result := (A[MiddleLo] + A[MiddleLo + 1]) / 2.0;
end;

function Median(const A: array of Integer): Extended; overload;
begin
  Assert(Length(A) > 0);
  // optimisations for array lengths 1 & 2 to avoid sorting
  if Length(A) = 1 then
    Exit(A[0]);
  if Length(A) = 2 then
    Exit((A[0] + A[1]) / 2);
  Generics.Collections.TArray.Sort<Integer>(A); // using standard comparer
  var MiddleLo := Length(A) div 2;
  if Odd(Length(A)) then
    Result := A[MiddleLo + 1]
  else
    Result := (A[MiddleLo] + A[MiddleLo + 1]) / 2;
end;

This issue was extracted from issue #16

@delphidabbler delphidabbler self-assigned this Jan 10, 2025
@delphidabbler delphidabbler added enhancement New feature or request considering Issue is currently under consideration labels Jan 10, 2025
@github-project-automation github-project-automation bot moved this to Considering in Code Snippets Jan 10, 2025
@delphidabbler
Copy link
Owner Author

The implementations proposed in the above comment was incorrect. The versions actually added to the collection are correct.

@delphidabbler delphidabbler added in progress Work has started on this issue and removed considering Issue is currently under consideration labels Jan 10, 2025
@delphidabbler delphidabbler moved this from Considering to In progress in Code Snippets Jan 10, 2025
@delphidabbler
Copy link
Owner Author

Implemented by merge commit 0bbdeb5

@delphidabbler delphidabbler added completed Issue completed and committed to develop. To be closed on next release and removed in progress Work has started on this issue labels Jan 10, 2025
@delphidabbler delphidabbler moved this from In progress to Completed in Code Snippets Jan 10, 2025
@delphidabbler delphidabbler added this to the Next Release milestone Jan 10, 2025
@delphidabbler delphidabbler removed this from the Next Release milestone Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed Issue completed and committed to develop. To be closed on next release enhancement New feature or request
Projects
Status: Completed
Development

No branches or pull requests

1 participant