如何找到排序数组的模式?

时间:2012-02-16 17:47:43

标签: c++ arrays algorithm sorting

我需要编写一个函数来查找数组的模式。我不擅长提出算法,但我希望其他人知道如何做到这一点。

我知道数组的大小和每个元素中的值,并且我将数组从最小到最大排序。

数组将传递给模式函数,如

mode = findMode(arrayPointer,sizePointer);

更新:

阅读评论后我试过这个

int findMode(int *arrPTR, const int *sizePTR)
{
    int most_found_element = arrPTR[0];
    int most_found_element_count = 0;
    int current_element = arrPTR[0];
    int current_element_count = 0;
    int count;
    for (count = 0; count < *sizePTR; count++)
    {
        if(count == arrPTR[count])
             current_element_count++;
        else if(current_element_count > most_found_element)
        {
            most_found_element = current_element;
            most_found_element_count = current_element_count;
        }
        current_element = count;
        current_element_count=1;
    }

    return most_found_element;
}

如果有人能把我排除在外,我仍然难以掌握这个算法。 我从来没有使用过矢量,所以不太了解其他的例子。

4 个答案:

答案 0 :(得分:7)

你几乎拥有一切。

您可以利用数组已排序的事实。

通过数组跟踪当前相等的连续数字,以及您在此之前找到的最大等数连续数字(以及哪个数字)制作它)。最后,您将获得最多的相等连续数字,并产生哪个数字。这将是模式。

注意:对于要求对数组进行排序的解决方案,请参阅one based in the histogram approach中的related question

答案 1 :(得分:5)

set most_found_element to the first element in the array
set most_found_element_count to zero
set current_element to the first element of the array
set current_element_count to zero
for each element e in the array
    if e is the same as the current_element
        increase current_element_count by one
    else
        if current_element_count is greater than most_found_element_count
            set most_found_element to the current_element
            set most_found_element_count to current_element_count
        set current_element to e
        set current_element_count to one
if current_element_count is greater than most_found_element_count
    set most_found_element to the current_element
    set most_found_element_count to current_element_count
print most_found_element and most_found_element_count

我认为名字会解释它,但我们走了:

When we start, no element has been found the most times
  so the "high-score" count is zero.
Also, the "current" value is the first, but we haven't looked at it yet 
  so we've seen it zero times so far
Then we go through each element one by one
  if it's the same as "current" value, 
     then add this to the number of times we've seen the current value.
  if we've reached the next value, we've counted all of the "current" value.
     if there was more of the current value than the "high-score"
        then the "high-score" is now the current value
     and since we reached a new value
        the new current value is the value we just reached
Now that we've seen all of the elements, we have to check the last one
  if there was more of the current value than the "high-score"
    then the "high-score" is now the current value
Now the "high-score" holds the one that was in the array the most times!

另请注意:我的原始算法/代码有一个错误,我们必须在循环结束后对“当前”进行额外检查,因为它永远不会找到“最后一个”。

答案 2 :(得分:3)

提示:

问:你如何定义模式?

答:数组中计数最大的数字。

问:如何计算有序数组中的数字?

A:遍历数组,当下一项等于前一项时,增加该值的计数。

问:如果前一个值的计数小于当前值的计数,那么之前的值可以是模式吗?

答:没有

答案 3 :(得分:0)

如果对输入数组进行了排序,这里采用与其他答案中描述的方法相同的方法,但以不同的方式实现,这是一种更好,更易于理解的方法。

  1. 在输入数组上运行循环。
  2. 保持全局模式值和模式计数。
  3. 运行一个滑动窗口,直到找到相同的元素。
  4. 如果本地相等元素数大于全局模式数,则更新全局模式和模式计数。
  5. 以下是C++中的工作和测试代码。

    int mode(vector<int> a, int N)
    {
        int mode = a[0];
        int mode_count = 1;
        int i = 0;
        while (i < N - 1) {
            int cur = a[i];
            int cur_count = 1;
            while (a[i] == a[i + 1]) {
                i++;
                cur_count++;
            }
            if (cur_count > mode_count) {
                mode_count = cur_count;
                mode = a[i];
            }
            i++;
        }
        return mode;
    }