mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
use median of five
Median5 algorithm taken from: Fast Deterministic Selection by Andrei Alexandrescu - 2016
This commit is contained in:
parent
c0347030c7
commit
c86f767cac
1 changed files with 23 additions and 10 deletions
|
|
@ -19,22 +19,35 @@ static inline void swap(TYPE *a, int i, int j)
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TYPE>
|
template <typename TYPE>
|
||||||
static inline void sort(TYPE *a, int i, int j, int k)
|
static inline void median(TYPE *A, int a, int b, int c, int d, int e)
|
||||||
{
|
{
|
||||||
if (a[i] > a[j])
|
if (A[c] < A[a])
|
||||||
swap(a, i, j);
|
swap(A, a, c);
|
||||||
if (a[i] > a[k])
|
if (A[d] < A[b])
|
||||||
swap(a, i, k);
|
swap(A, b, d);
|
||||||
if (a[j] > a[k])
|
if (A[d] < A[c]) {
|
||||||
swap(a, j, k);
|
swap(A, c, d);
|
||||||
|
swap(A, a, b);
|
||||||
|
}
|
||||||
|
if (A[e] < A[b])
|
||||||
|
swap(A, b, e);
|
||||||
|
if (A[e] < A[c]) {
|
||||||
|
swap(A, c, e);
|
||||||
|
if (A[c] < A[a])
|
||||||
|
swap(A, a, c);
|
||||||
|
} else if (A[c] < A[b]) {
|
||||||
|
swap(A, b, c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TYPE>
|
template <typename TYPE>
|
||||||
static void partition(TYPE *a, int &l, int &h)
|
static void partition(TYPE *a, int &l, int &h)
|
||||||
{
|
{
|
||||||
int m = l + (h - l) / 2;
|
int half = (h - l) / 2;
|
||||||
sort(a, l, m, h);
|
int quarter = (h - l) / 4;
|
||||||
TYPE pivot = a[m];
|
int middle = l + half;
|
||||||
|
median(a, l, l + quarter, middle, middle + quarter, h);
|
||||||
|
TYPE pivot = a[middle];
|
||||||
for (int i = l; i <= h;)
|
for (int i = l; i <= h;)
|
||||||
if (a[i] < pivot)
|
if (a[i] < pivot)
|
||||||
swap(a, i++, l++);
|
swap(a, i++, l++);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue