• Kuan-Wei Chiu's avatar
    platform/chrome: sensorhub: Implement quickselect for median calculation · d131f1f3
    Kuan-Wei Chiu authored
    The cros_ec_sensor_ring_median function currently uses an inefficient
    sorting algorithm (> O(n)) to find the median of an array. This patch
    replaces the sorting approach with the quickselect algorithm, which
    achieves an average time complexity of O(n).
    
    The algorithm employs the median-of-three rule to select the pivot,
    mitigating worst-case scenarios and reducing the expected number of
    necessary comparisons. This strategy enhances the algorithm's
    efficiency and ensures a more balanced partitioning.
    
    In the worst case, the runtime of quickselect could regress to O(n^2).
    To address this, alternative algorithms like median-of-medians that
    can guarantee O(n) even in the worst case. However, due to higher
    overhead and increased complexity of implementation, quickselect
    remains a pragmatic choice for our use case.
    Signed-off-by: default avatarKuan-Wei Chiu <visitorckw@gmail.com>
    Link: https://lore.kernel.org/r/20231110165314.1559285-1-visitorckw@gmail.comSigned-off-by: default avatarTzung-Bi Shih <tzungbi@kernel.org>
    d131f1f3
cros_ec_sensorhub_ring.c 31.3 KB