mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
got rid of the recursion
This commit is contained in:
parent
eb968e50b6
commit
b0c9bc7d41
1 changed files with 10 additions and 8 deletions
|
|
@ -33,14 +33,16 @@ static int partition(TYPE *a, int l, int h)
|
|||
template <typename TYPE>
|
||||
static TYPE select(TYPE *a, int l, int h, int k)
|
||||
{
|
||||
if (l == h)
|
||||
return a[l];
|
||||
int i = partition(a, l, h);
|
||||
if (i == k)
|
||||
return a[i];
|
||||
if (i > k)
|
||||
return select(a, l, i-1, k);
|
||||
return select(a, i+1, h, k);
|
||||
while (l < h) {
|
||||
int i = partition(a, l, h);
|
||||
if (k == i)
|
||||
return a[k];
|
||||
if (k < i)
|
||||
h = i - 1;
|
||||
else
|
||||
l = i + 1;
|
||||
}
|
||||
return a[l];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue