Fastest code C/C++ to select the median in a set of 27 floating point values

后端 未结 15 1142
梦谈多话
梦谈多话 2020-12-07 09:24

This is the well know select algorithm. see http://en.wikipedia.org/wiki/Selection_algorithm.

I need it to find the median value of a set of 3x3x3 voxel values. Sinc

15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 09:43

    I suppose your best bet is to take an existing sorting algorithm and try to figure out whether you can adapt it so that the set does not need to be fully sorted. For determining the median, you need at most half the values sorted, either the lower or higher half would be enough:

    original:              | 5 | 1 | 9 | 3 | 3 |
    sorted:                | 1 | 3 | 3 | 5 | 9 |
    lower half sorted:     | 1 | 3 | 3 | 9 | 5 |
    higher half sorted:    | 3 | 1 | 3 | 5 | 9 |
    

    The other half would be a bucket of unsorted values that merely share the property of being larger/smaller or equal to the largest/smallest sorted value.

    But I have no ready algorithm for that, it's just an idea of how you might take a short-cut in your sorting.

提交回复
热议问题