1. algorithm concept Problem description: Find the I-th small element from the array (where there are no repeated elements in the array ), this is a classic question about "selection in expected linear time. Idea: Introduction to algorithms 215 page 9.2 Selection in each CT linear time2. Java implementation idea: Introduction to algorithms 216 page pseudocode
/* The algorithm is expected to be used for Linear Time Selection. input requirements: No repeated elements in array */public static int randomizedselect (INT [] array, int start, int end, int I) {If (START = END) {return array [start];} int middle = randomizedpartition (array, start, end); int K = middle-start + 1; if (I = k) {return array [Middle];} else if (I <k) {return randomizedselect (array, start, middle-1, I );} else {return randomizedselect (array, middle + 1, end, I-k );}}
Randomizedpartition (array, start, end) See [Introduction to algorithms-010] Quick Sort)
3. Non-recursive version
/* Linear Time Selection for non-recursive versions */public static int iterativerandomizedselect (INT [] array, int start, int end, int I) {int result = 0; while (start <= END) {If (START = END) {return array [start];} int middle = randomizedpartition (array, start, end ); int K = middle-start + 1; if (I = k) {result = array [I];} else if (I <k) {end = middle-1 ;} else {start = middle + 1; I = I-K ;}} return result ;}