select.c
53 lines
| 960 B
| text/x-c
|
CLexer
r0 | /* $Id: select.c 3304 2011-01-17 15:25:59Z brideout $ */ | |||
#define SWAP(a,b) temp=(a);(a)=(b);(b)=temp; | ||||
/*********************************************************************** | ||||
* | ||||
* dselect | ||||
* | ||||
*/ | ||||
double | ||||
dselect(int k, int n, double *arr) | ||||
{ | ||||
int i,ir,j,l,mid; | ||||
double a, temp; | ||||
l=1; | ||||
ir=n; | ||||
for (;;) { | ||||
if (ir <= l+1) { | ||||
if (ir == l+1 && arr[ir] < arr[l]) { | ||||
SWAP(arr[l],arr[ir]) | ||||
} | ||||
return arr[k]; | ||||
} else { | ||||
mid=(l+ir) >> 1; | ||||
SWAP(arr[mid],arr[l+1]) | ||||
if (arr[l+1] > arr[ir]) { | ||||
SWAP(arr[l+1],arr[ir]) | ||||
} | ||||
if (arr[l] > arr[ir]) { | ||||
SWAP(arr[l],arr[ir]) | ||||
} | ||||
if (arr[l+1] > arr[l]) { | ||||
SWAP(arr[l+1],arr[l]) | ||||
} | ||||
i=l+1; | ||||
j=ir; | ||||
a=arr[l]; | ||||
for (;;) { | ||||
do i++; while (arr[i] < a); | ||||
do j--; while (arr[j] > a); | ||||
if (j < i) break; | ||||
SWAP(arr[i],arr[j]) | ||||
} | ||||
arr[l]=arr[j]; | ||||
arr[j]=a; | ||||
if (j >= k) ir=j-1; | ||||
if (j <= k) l=i; | ||||
} | ||||
} | ||||
} | ||||
#undef SWAP | ||||
/* (C) Copr. 1986-92 Numerical Recipes Software *1(.(1,4. */ | ||||