| 1 |
# -*- org -*- |
| 2 |
#+CATEGORY: sort |
| 3 |
|
| 4 |
* Routines for selecting the k largest or smallest values could use |
| 5 |
heapsort for speed O(N log k) rather than insertion O(N k). |
| 6 |
|
| 7 |
* Sorting of complex arrarys without using additional memory. We try |
| 8 |
to avoid allocating memory internally in GSL, so internally computing |
| 9 |
the magnitudes and storing them in a temporary array is undesirable. |
| 10 |
|
| 11 |
Obviously a complex array can be sorted using sqrt(x*x + y*y) <=> |
| 12 |
sqrt(u*u + v*v) (written in a numerically stable way) for every |
| 13 |
comparison, but this may be unacceptably slow. Maybe not? It is just a |
| 14 |
constant factor. The square roots could sometimes be avoided by |
| 15 |
optimization, |
| 16 |
|
| 17 |
(x,y) = (MAX(|x|,|y|), MIN(|x|,|y|)) |
| 18 |
(u,v) = (MAX(|u|,|v|), MIN(|u|,|v|)) |
| 19 |
|
| 20 |
if (x < u/sqrt(2)) /* This part is optional optimization */ |
| 21 |
return -1 |
| 22 |
if (x > u*sqrt(2)) |
| 23 |
return +1 |
| 24 |
|
| 25 |
if (x == 0 && u == 0) ... |
| 26 |
if (x == 0) ... |
| 27 |
if (u == 0) ... |
| 28 |
|
| 29 |
t = u*sqrt((1+(v/u)^2)/(1+(y/x)^2)) |
| 30 |
|
| 31 |
if (x < t) |
| 32 |
return -1 |
| 33 |
if (x > t) |
| 34 |
return +1 |
| 35 |
else |
| 36 |
return 0 |
| 37 |
|
| 38 |
but this does depend on the data having sufficient range for the |
| 39 |
optimization to be worthwhile, otherwise it is an extra cost. |