从N个元素中选择第i小的元素

时常在笔试,面试题中看到这个问题,《算法导论》中给出了很好的解答。

Selection of the ith smallest element of the array A can be done in θ(n) times.

The psuedocode is following:

Code
Randomized_Select(A,p,r,i)
{
    if p==r
        then
return A[p]
    q
=Randomized_Partition(A,p,r)
    k
=q-p+1
    if i==k
        then
return A[q]
    else if i<k
        then
return Randomized_select(A,p,q-1,i)
    else
        return Randomized_select(A,q+1,r,i-k)
}


=====================================
Randomized_Partition(A,p,r)
{
    i
=RANDOW(p,r)
    exchange A[r] ↔ A[i]
    return Partition(A,p,r)
}
=====================================
Partition(A,p,r)
{
    x
=A[r]
    i
=p-1
    for j=p to r-1
        if A[j]<=x
              then i
=i+1
                  exchange A[i]↔A[j]
    exchange A[i
+1] ↔A[r]
    return i+1
}
 
 

 

Transfer to C# code:

 

Code
namespace SelectMinimum
{
class Program
{
static void Main(string[] args)
{
int[] A = new int[] {0,25,12,14,57,45,18,75,85,74,45,63,35,28,39 };
SelectSort ss
= new SelectSort();
int result;
result
=ss.Randomized_Select(A,0,14,11);
Console.WriteLine(result);
Console.ReadLine();
}


}

public class SelectSort
{
public int Randomized_Select(int[] A, int p, int r, int i)
{
if (p == r)
return A[p];
int q = Randomize_Partition(A, p, r);
int k = q - p + 1;
if (i == k)
return A[q];
else if (i < k)
return Randomized_Select(A, p, q - 1, i);
else
return Randomized_Select(A, q + 1, r, i - k);

}

public int Randomize_Partition(int[] A, int p, int r)
{
Random rd
= new Random();
int i = rd.Next(p, r);
int temp;
temp
= A[r];
A[r]
= A[i];
A[i]
= temp;
return Partition(A, p, r);
}

public int Partition(int[] A, int p, int r)
{
int x = A[r];
int i = p - 1;
for (int j = p; j <= r - 1; j++)
{
if (A[j] <= x)
{
i
+= 1;
int temp;
temp
= A[i];
A[i]
= A[j];
A[j]
= temp;
}
}
int temp2;
temp2
= A[i + 1];
A[i
+ 1] = A[r];
A[r]
= temp2;
return i + 1;
}
}
}

转载于:https://www.cnblogs.com/ision/archive/2008/11/05/1327313.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值