Skip to main content
14 votes

Calculating the median and mode of a list of integers

Your Code Is Buggy. Test It! Try this testcase: assert_eq!(exercise_median_of_vector(&mut Vec::from([1])), 1); Always write test cases for your code! In a ...
Davislor's user avatar
  • 9,115
8 votes

Computing mean and median

I have a confusion about this temps[temps.size()/2] when temps.size() is an odd number. How does the "vector::operator[]" handle ...
Snowhawk's user avatar
  • 6,770
8 votes

Finding median of sub arrays taking too much time

Not related to performance, but never ever do this: #include <bits/stdc++.h> using namespace std; For reference see these Q&A at Stack Overflow: Why ...
πάντα ῥεῖ's user avatar
7 votes
Accepted

Finding median from unsorted array with duplicate elements without sorting it

If we never modify the elements of array, then it should be passed as a pointer to const: int const *array. The frequency ...
Toby Speight's user avatar
  • 88.3k
7 votes
Accepted

generic implementation of median

It modifies the input I think it is surprising that an operation that is intended to just give you some statistics back is going to modify the input. In the test code you also test it on ...
G. Sliepen's user avatar
  • 69.2k
7 votes

Find median of two sorted arrays, with complexity of min(log(m),log(n))

Consistent whitespace cout << "\nthe median of the merged array is : " << findMedian(a, b, m, n)<< '\n'; isn't even consistent within the ...
Peter Taylor's user avatar
  • 24.5k
7 votes

Computing mean and median

To address your confusion, there is no floating point values. Both temps.size() and 2 are integrals, so the division is an ...
vnp's user avatar
  • 58.7k
7 votes

Find median of two sorted ranges

While this is not a thorough code review, here are some things you could improve or should pay attention to: median2 is dangerous for containers which offer ...
Ben Steffan's user avatar
  • 5,333
7 votes
Accepted

Median of two sorted arrays in Python

mentioned that \$O(\log \min(n,m))\$ would be really outside of what I can understand with my current knowledge. Nah! It's really about the sizes \$n, m\$, rather than about the algorithm. If one is ...
J_H's user avatar
  • 42.1k
6 votes

Find median of two sorted ranges

Correctness &item - &*begin1 sets you on the fast track for undefined behavior, as the value returned by *begin1 might ...
hoffmale's user avatar
  • 6,528
6 votes
Accepted

Generic implementation of median #2: Follow up

Be as generic as possible Your median() and median_in_place() functions don't work on all things that are ranges, because you ...
G. Sliepen's user avatar
  • 69.2k
5 votes

generic implementation of median

Performance benchmarking on std::nth_element + std::max_element vs std::partial_sort_copy. ...
Oliver Schönrock's user avatar
5 votes
Accepted

Finding median of sub arrays taking too much time

One performance problem is the overhead of allocating and resizing a vector. Since you read the size up front, why not simply reserve() enough space? ...
uli's user avatar
  • 1,755
5 votes

Median of two sorted arrays

According to the Style Guide for Python Code, function and variable names should be “lowercase, with words separated by underscores as necessary to improve readability.” In your case I'd suggest <...
Martin R's user avatar
  • 24.2k
5 votes
Accepted

Median of numbers stored in array

1. Arraymake: Change the name to MakeArray as it makes more sense or name it ...
Jakub Dąbek's user avatar
5 votes

Find median of two sorted ranges

Bug I don't think your algorithm is correct. For example, try these two test cases: ...
JS1's user avatar
  • 28.9k
5 votes
Accepted

Median Calculation of List of Integers without using heap

Performance I instrumented your code differently: ...
Toby Speight's user avatar
  • 88.3k
5 votes
Accepted

Compute rolling median absolute deviation (MAD) in R

Is there a more efficient way to calculate the rolling MAD while automatically ignoring previously detected outliers? Near as I can tell, that poor median() function keeps considering each slightly ...
J_H's user avatar
  • 42.1k
4 votes

Median Calculation of List of Integers without using heap

If you want to time something, you should time just that. When I ran your original code, I got 6.023478031158447 seconds. When I instead did ...
Acccumulation's user avatar
4 votes

Computing mean and median

Concerning your question: When dividing an int by an int, or when converting double to int, the result is always an int. This means that the result is being rounded down, never up. The part after the ...
Raimund Krämer's user avatar
4 votes
Accepted

Median of a Vector assignment

#include "../../std_lib_facilities.h" While PPP is structured around using this header, you will eventually be including only what you really need to make your ...
Snowhawk's user avatar
  • 6,770
4 votes
Accepted

Find Median of Two Sorted Arrays in JS

The 99th percentile is due to the linear nature of your approach. The goal of this exercise is to figure out a logarithmic one. I don't want to spell out the algorithm entirely. Just a hint to get ...
vnp's user avatar
  • 58.7k
4 votes

Rolling median function (Daily Coding Problem 377)

This compiles cleanly and produces reasonable integer output (though not in the format required by the problem statement). Note that you are required to include ...
Toby Speight's user avatar
  • 88.3k
4 votes
Accepted

Find median of values without copying

Answers to your questions Have I omitted any useful tests? Yes. While you did test the edge case of an empty range, there are other edge cases and extreme values that you should test. For example, the ...
G. Sliepen's user avatar
  • 69.2k
4 votes
Accepted

Median cut algorithm

You probably shouldn't award this a bounty; I'm just using your question as an excuse to learn Rust. Specifically, my understanding of how the [de]referencing works is clunky at best. Does it run? In ...
ShapeOfMatter's user avatar
4 votes

Median cut algorithm

/* Hello and welcome to the Rust community! First, a couple of ideas after briefly looking through the code. The algorithm can be optimized with SIMD operations. Read up on SIMD in Rust if you wish ...
Peter Blackson's user avatar
4 votes

Median of two sorted arrays in Python

The previous answer addresses your main concern about complexity. This will address one of the now-deleted comments on the question regarding functionality. Portability The function that was posted ...
toolic's user avatar
  • 15.7k
3 votes

Rolling median function (Daily Coding Problem 377)

My main issue is that the median function takes a C-style array. Generally C-style arrays are avoided in modern C++. It would be much better if it took an std::array...
nivag's user avatar
  • 401
3 votes

Calculate a median

While there is nothing wrong with defining your class method as private, I'm going to demonstrate with public for simplicity. I ...
mickmackusa's user avatar
  • 8,802
3 votes

Generic implementation of median #2: Follow up

Instead of using std::midpoint(), it might be better to allow types to have their own midpoint() function (in their namespace), ...
Toby Speight's user avatar
  • 88.3k

Only top scored, non community-wiki answers of a minimum length are eligible