Blame sort/TODO

Packit 67cb25
# -*- org -*-
Packit 67cb25
#+CATEGORY: sort
Packit 67cb25
Packit 67cb25
* Routines for selecting the k largest or smallest values could use
Packit 67cb25
heapsort for speed O(N log k) rather than insertion O(N k).
Packit 67cb25
Packit 67cb25
* Sorting of complex arrarys without using additional memory. We try
Packit 67cb25
to avoid allocating memory internally in GSL, so internally computing
Packit 67cb25
the magnitudes and storing them in a temporary array is undesirable. 
Packit 67cb25
Packit 67cb25
Obviously a complex array can be sorted using sqrt(x*x + y*y) <=>
Packit 67cb25
sqrt(u*u + v*v) (written in a numerically stable way) for every
Packit 67cb25
comparison, but this may be unacceptably slow. Maybe not? It is just a
Packit 67cb25
constant factor. The square roots could sometimes be avoided by
Packit 67cb25
optimization,
Packit 67cb25
Packit 67cb25
    (x,y) = (MAX(|x|,|y|), MIN(|x|,|y|))
Packit 67cb25
    (u,v) = (MAX(|u|,|v|), MIN(|u|,|v|))
Packit 67cb25
Packit 67cb25
    if (x < u/sqrt(2))     /* This part is optional optimization */
Packit 67cb25
       return -1 
Packit 67cb25
    if (x > u*sqrt(2))
Packit 67cb25
       return +1
Packit 67cb25
Packit 67cb25
    if (x == 0 && u == 0) ...
Packit 67cb25
    if (x == 0) ...
Packit 67cb25
    if (u == 0) ...    
Packit 67cb25
Packit 67cb25
    t = u*sqrt((1+(v/u)^2)/(1+(y/x)^2))
Packit 67cb25
    
Packit 67cb25
    if (x < t)
Packit 67cb25
       return -1
Packit 67cb25
    if (x > t)
Packit 67cb25
       return +1 
Packit 67cb25
    else
Packit 67cb25
       return 0
Packit 67cb25
Packit 67cb25
but this does depend on the data having sufficient range for the
Packit 67cb25
optimization to be worthwhile, otherwise it is an extra cost.