mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
use insertion sort for small ranges
This commit is contained in:
parent
cf60e38b9d
commit
f83ca91608
1 changed files with 33 additions and 11 deletions
44
quick.hh
44
quick.hh
|
|
@ -57,14 +57,30 @@ static void partition(TYPE *a, int &l, int &h)
|
|||
++i;
|
||||
}
|
||||
|
||||
template <typename TYPE>
|
||||
static void insertion(TYPE *a, int n)
|
||||
{
|
||||
for (int i = 1, j; i < n; ++i) {
|
||||
TYPE t = a[i];
|
||||
for (j = i; j > 0 && t < a[j-1]; --j)
|
||||
a[j] = a[j-1];
|
||||
a[j] = t;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename TYPE>
|
||||
static void sort(TYPE *a, int l, int h)
|
||||
{
|
||||
if (l < h) {
|
||||
int lt = l, gt = h;
|
||||
partition(a, lt, gt);
|
||||
sort(a, l, lt - 1);
|
||||
sort(a, gt + 1, h);
|
||||
int n = h - l + 1;
|
||||
if (n <= 32) {
|
||||
insertion(a+l, n);
|
||||
} else {
|
||||
int lt = l, gt = h;
|
||||
partition(a, lt, gt);
|
||||
sort(a, l, lt - 1);
|
||||
sort(a, gt + 1, h);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -72,14 +88,20 @@ template <typename TYPE>
|
|||
static void select(TYPE *a, int l, int h, int k)
|
||||
{
|
||||
while (l < h) {
|
||||
int lt = l, gt = h;
|
||||
partition(a, lt, gt);
|
||||
if (k < lt)
|
||||
h = lt - 1;
|
||||
else if (k > gt)
|
||||
l = gt + 1;
|
||||
else
|
||||
int n = h - l + 1;
|
||||
if (n <= 32) {
|
||||
insertion(a+l, n);
|
||||
break;
|
||||
} else {
|
||||
int lt = l, gt = h;
|
||||
partition(a, lt, gt);
|
||||
if (k < lt)
|
||||
h = lt - 1;
|
||||
else if (k > gt)
|
||||
l = gt + 1;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue