Algorithm 489: the algorithm SELECT—for finding the ith smallest of n elements [M1]
暂无分享,去创建一个
<italic>SELECT</italic> will rearrange the values of array segment <italic>X</italic>[<italic>L</italic>: <italic>R</italic>] so that <italic>X</italic>[<italic>K</italic>] (for some given <italic>K</italic>; <italic>L</italic> ≤ <italic>K</italic> ≤ <italic>R</italic>) will contain the (<italic>K</italic>-<italic>L</italic>+1)-th smallest value, <italic>L</italic> ≤ <italic>I</italic> ≤ <italic>K</italic> will imply <italic>X</italic>[<italic>I</italic>] ≤ <italic>X</italic>[<italic>K</italic>], and <italic>K</italic> ≤ <italic>I</italic> ≤ <italic>R</italic> will imply <italic>X</italic>[<italic>I</italic>] ≥ <italic>X</italic>[<italic>K</italic>. While <italic>SELECT</italic> is thus functionally equivalent to Hoare's algorithm <italic>FIND</italic> [1], it is significantly faster on the average due to the effective use of sampling to determine the element <italic>T</italic> about which to partition <italic>X</italic>. The average time over 25 trials required by <italic>SELECT</italic> and <italic>FIND</italic> to determine the median of <italic>n</italic> elements was found experimentally to be: <italic>n</italic> 500 1000 5000 10000 <italic>SELECT</italic> 89 ms. 141 ms. 493 ms. 877 ms. <italic>FIND</italic> 104 ms. 197 ms. 1029 ms. 1964 ms. The arbitrary constants 600, .5, and .5 appearing in the algorithm minimize execution time on the particular machine used. <italic>SELECT</italic> has been shown to run in time asymptotically proportional to <italic>N</italic> + min (<italic>I</italic>, <italic>N</italic>-<italic>I</italic>), where <italic>N</italic> = <italic>L</italic> - <italic>R</italic> + 1 and <italic>I</italic> = <italic>K</italic> - <italic>L</italic> + 1. A lower bound on the running time within 9 percent of this value has also been proved [2]. Sites [3] has proved <italic>SELECT</italic> terminates.
[1] C. A. R. Hoare. Algorithm 63: partition , 1961, CACM.
[2] Richard L. Sites. Some thoughts on proving clean termination of programs. , 1974 .
[3] Ronald L. Rivest,et al. Expected time bounds for selection , 1975, Commun. ACM.