int select_kth_largest(int k, int *S, int n) { /* return the kth largest element from the ordered set S of integers */ /* n is the number of elements in S */ /* Algorithm : if n < 50 then sort in dec. order S, return kth largest element in S else divide S into ceil(n/5) sequences of 5 elements each with upto four leftover elements sort in dec order each 5 element sequence let M be the sequence of medians of the 5 element sets m = select_kth_largest(ceil(n(M)/2), M, n/5); let S1, S2 and S3 be the sequences of elements in S greater than, equal to and less than m respectively. if n(S1) >=k then return select_kth_largest(k, S1, n(S1)); else if (n(S1) + n(S2) >= k) then return m else return select_kth_largest (k - n(S1) - n(S2), S3);