Procedure: GM
Input: For each j ∈ {1 .. J}, a column {ai,j | i = TOPj .. BOTj} of nondecreasing elements, where TOPj <= BOTj are indexes of the column ranges under consideration. A number I between 1 and the total number of elements ∑j=1J(BOTj - TOPj + 1) in the columns.
Output: An Ith smallest element η, and, for each j ∈ {1..J}, a partition of each range {TOPj .. BOTj} into three new ranges, {TOPj1 .. BOTj1} with value less than η, {TOPj2 .. BOTj2} with value equal to η, {TOPj3 .. BOTj3} with value greater than η.
Set Tj = TOPj and Bj = BOTj for each j ∈ {1 .. J}.
Do forever
Set Mj = Bj - Tj + 1 for each j ∈ {1 .. J}. Set S = ∑j=1JMj.
For each j ∈ {1 .. J}, find the median element ηj of the set {ai,j | i = Tj .. Bj}. Sort the medians in nondecreasing order, so that ηj1 <= ηj2 <= .. <= ηjJ.
Compute the value k such that ∑j=1k-1Mj < S/2 <= ∑j=1kMj. Set η = ηk.
Compute for each j ∈ {1 .. J}, TTj = min{i | (TOPj <= i <= BOTj) ∧ (ai,j = η)}, and BBj = max{i | (TOPj <= i <= BOTj) ∧ (ai,j = η)}.
Set M1 = ∑j=1J(TTj - Tj) and M2 = ∑j=1J(BBj - Tj + 1).
If (M1 < I <= M2) then begin
η is an Ith smallest element. Set TOPj1 = TOPj, BOTj1 = TTj - 1, TOPj2 = TTj, BOTj2 = BBj, TOPj3 = BBj + 1, BOTj3 = BOTj.
Halt.
End
If (M1 >= I) then set Bj = TTj.
If (M2 < I) then set Tj = BBj + 1 and decrement I by M2.
End do
End GM