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

2023-09-11 02:40:00 作者:听心痛的声音

我需要编写一个函数来找到一个阵列模式。我不过在未来与算法并不好,我希望其他人知道如何做到这一点。

我知道数组的大小和每个元素的值,我有数组排序从低到最大的。

数组将被传递到模式功能像

模式= 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;
    诠释计数;
    为(计数= 0; COUNT< * sizePTR;计数++)
    {
        如果(计数== arrPTR [计数])
             current_element_count ++;
        否则,如果(current_element_count> most_found_element)
        {
            most_found_element = current_element;
            most_found_element_count = current_element_count;
        }
        current_element =计数;
        current_element_count = 1;
    }

    返回most_found_element;
}
 

我还是有把握这种算法但如果任何人都可以整理出我的问题。 我从来没有使用矢量所以并不真正了解的其他例子。

解决方案

 设置most_found_element到阵列中的第一个元素
设置most_found_element_count为零
设置current_element到阵列的第一个元素
设置current_element_count为零
对于每个元素e的阵列中
    如果e是一样的current_element
        加一current_element_count
    其他
        如果current_element_count大于most_found_element_count
            集most_found_element到current_element
            设置most_found_element_count为current_element_count
        设置current_element到e
        设置current_element_count一
如果current_element_count大于most_found_element_count
    集most_found_element到current_element
    设置most_found_element_count为current_element_count
打印most_found_element和most_found_element_count
 

我想的名字可以解释,但在这里我们去:

 当我们开始,没有元素被发现的次数最多
  因此,高得分计数为零。
此外,当前值是第一个,但我们还没看它尚未
  因此,我们已经看到了它的零次为止
然后,我们通过每个元素逐个
  如果是一样的当前值,
     然后将它添加到的时候,我们已经看到了当前值的数量。
  如果我们已经到达了下一个值,我们数了所有的当前值。
     如果有更多的电流值比高得分
        然后高得分是现在的电流值
     因为我们达到了一个新的价值
        新的当前值是我们刚刚达成的价值
现在,我们已经看到了所有的元素,我们要检查的最后一
  如果有更多的电流值比高得分
    然后高得分是现在的电流值
现在高得分保持,这就是一个阵列中的最倍!
 
在这个excel中的index函数,数组模式和引用模式有什么区别

另外请注意:我原来的算法/ code有一个bug,我们要做的当前额外的检查循环结束后,因为它从来没有发现在最后一个

I need to write a function to find the mode of a array. I'm not good at coming up with algorithms however and I'm hoping someone else knows how to do this.

I know the size of the array and the values in each element, and I have the array sorted from least to greatest.

array would be passed to the mode function like

mode = findMode(arrayPointer, sizePointer);

UPDATE:

After reading the comments I've tried this

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;
}

I'm still having problems grasping this algorithm though if anyone can sort me out. I've never used vectors so don't really understand the other examples.

解决方案

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

I thought the names would explain it, but here we go:

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!

Also note: my original algorithm/code had a bug, we have to do an extra check of "current" after the loop ends, as it never finds "the one after the last".