GM算法 -- 在n个已排序(非减)的列中寻找所有元素中第i小的元素

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, TOPj= 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值