在数组中寻找主要元素的算法及其正确性证明。

在数组中寻找主要元素的算法。

这个是数据结构与算法分析-C语言描述一书的课后习题2.19的解答。

长度为n的数组A的主要元素,指的是在所有元素中出现次数超过n/2次的元素。(所以最多有一个而已。)下面的算法是Google出来的答案(原来是伪代码,我用C#实现了),教材上给的提示实在是难于理解。

//  Let T [1..n] be an array of n integers. An integer is a majority element in T if it appears 
//  strictly more than n/2 times in T. This paper is concerned with finding an algorithm that 
//  can decide whether an array T [1..n] contains a majority element, and if so, find it. The 
//  algorithm runs in linear time in the worst case.  
//  Also, the only comparisons allowed between the elements of T are tests of equality.  This 
//  means, there does not exist an order relation between the elements of the array.  
//  Input  : An input array, unsorted, of the type T [i..j].  
//  Output : The majority element ele, if it exists. If a majority element does not exist, the algorithm returns a null.
public   static  Object FindMajority(Int32[] Input)
{
    Int32 candidate 
= FindElement(Input);
    Int32 count 
= 0;
    
    
for(Int32 i = 0; i < Input.Length; i++)
        
if (Input[i] == candidate) count++;

    
if (count > Input.Length / 2return candidate;
    
else return null;
}



private   static  Int32 FindElement(Int32[] A)
{
    Int32 m 
= 0;    // marker
    Int32 ele = 0;
    
for (Int32 x = 0; x < A.Length; x++)
    
{
        
if (m == 0)  
        
{
            m 
= 1;
            ele 
= A[x];
        }

        
else if (A[x] == ele) m++;
        
else m--;          
    }


    
return ele; // The candidate
}


从直接上很容易感觉这个算法是正确的,证明的大致思路就是如果一个元素符合条件,那么最后返回的值就一定是它。因为它的权值是最大的,如果没有元素符合条件,那么就不会通过 FindMajority中的测试。

这个算法的复杂度明显为O(n)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值