首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Boyer多数投票算法的记忆复杂性?

Boyer多数投票算法的记忆复杂性?
EN

Stack Overflow用户
提问于 2016-08-27 14:48:50
回答 1查看 434关注 0票数 0

根据地雷的理解,找到多数元素Boyer-Moore多数投票算法是O(1),即它是常数,而不是与输入的大小成正比。那么为什么wiki链接提到对数空间{\显示方式O(\log n)} O(\log n)

这里是供参考的程序

代码语言:javascript
复制
public class MajorityElement {
    /* Function to print Majority Element */
    void printMajority(int a[], int size) {
        /* Find the candidate for Majority */
        int cand = findCandidate(a, size);

        /* Print the candidate if it is Majority */
        if (isMajority(a, size, cand))
            System.out.println(" " + cand + " ");
        else
            System.out.println("No Majority Element");
    }

    /* Function to find the candidate for Majority */
    int findCandidate(int a[], int size) {
        int maj_index = 0, count = 1;
        int i;
        for (i = 1; i < size; i++) {
            if (a[maj_index] == a[i])
                count++;
            else
                count--;
            if (count == 0) {
                maj_index = i;
                count = 1;
            }
        }
        return a[maj_index];
    }

    /*
     * Function to check if the candidate occurs more than n/2 times
     */
    boolean isMajority(int a[], int size, int cand) {
        int i, count = 0;
        for (i = 0; i < size; i++) {
            if (a[i] == cand)
                count++;
        }
        if (count > size / 2)
            return true;
        else
            return false;
    }
EN

回答 1

Stack Overflow用户

发布于 2016-08-27 15:11:45

这是因为变量count需要O(log(n))位来存储候选对象的出现数。当然,在日常测试中,不太可能尝试使用超过2^32 (或类似的)单元格的数组。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39182402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档