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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
5
votes
generic implementation of median
Performance benchmarking on std::nth_element + std::max_element vs std::partial_sort_copy. ...
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?
...
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
<...
5
votes
Accepted
Median of numbers stored in array
1. Arraymake:
Change the name to MakeArray as it makes more sense or name it ...
5
votes
Find median of two sorted ranges
Bug
I don't think your algorithm is correct. For example, try these two test cases:
...
5
votes
Accepted
Median Calculation of List of Integers without using heap
Performance
I instrumented your code differently:
...
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 ...
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
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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...
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 ...
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), ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
median × 38c++ × 13
python × 9
algorithm × 7
performance × 6
programming-challenge × 6
array × 5
java × 4
c × 4
javascript × 3
time-limit-exceeded × 3
complexity × 3
statistics × 3
c# × 2
beginner × 2
python-3.x × 2
c++11 × 2
python-2.x × 2
rust × 2
generics × 2
binary-search × 2
heap × 2
constrained-templates × 2
php × 1
object-oriented × 1