2012-02-16 36 views
2

我需要編寫一個函數來查找數組的模式。我不擅長提出算法,但我希望別人知道如何做到這一點。如何找到排序數組的模式?

我知道數組的大小和每個元素中的值,並且我從最小到最大排序了數組。

陣列將被傳遞到模式功能等

模式= findMode(arrayPointer,sizePointer);

UPDATE:

閱讀評論之後,我已經試過抓住了這個算法,這

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

我仍然有問題,但如果任何人都可以整理我出去。 我從來沒有使用過載體,所以不太瞭解其他例子。

+3

你能描述一些你嘗試過的東西,也許發佈一些代碼嗎?這裏給你一個提示:你可能試着循環訪問數組,每次看到一個元素時,都會將一個元素特定的計數器加1。 – asf107 2012-02-16 17:49:35

+0

什麼是「陣列模式」? – wilhelmtell 2012-02-16 17:51:51

+4

你想要一個數組的_mode_嗎?你的意思是_mean_?或_median_?或者我需要學習一個新的術語?編輯:[模式是一件真實的事情! IR LERND](http://www.mathsteacher.com.au/year8/ch17_stat/02_mean/mean.htm) – 2012-02-16 17:52:00

回答

4
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! 

另外請注意:我原來的算法/代碼有一個bug,我們有在循環結束後做一次額外的「當前」檢查,因爲它永遠不會找到「最後一個」。

+0

我試過並在問題中發佈了代碼,但我仍然無法理解這一點。你能告訴我我做錯了什麼嗎? – sircrisp 2012-02-16 20:36:44

+0

@sircrisp:如果最高值是模式,我的算法中存在一個錯誤。另外,我添加了一個解釋。 – 2012-02-17 01:49:08

6

你幾乎擁有一切。

您可以利用數組排序的事實。

只需經過兩個當前等於連續編號的陣列跟蹤和最大數量相等的連續數字的你已經發現,直到這一點(和數量產生的話)。最後,你將獲得最多數量相等的連續數字以及哪個數字產生它。那將是模式。

注:對於那些不要求數組進行排序的溶液,參見例如one based in the histogram approachrelated question

2

提示:

問:你如何定義模式? A:在數組中數量最大的數字。

問:你如何計算有序數組中的數字?

答:遍歷數組,當下一個項目與前一個項目相同時,遞增該值的計數。

問:如果先前值的計數小於當前值的計數,那麼以前的值可以是模式嗎?

答:沒有

0

如果對輸入數組進行排序,這裏的方法與其他答案中所描述的方法相同,但是以不同的方式實現,這是一種更好且易於理解的方式。

  1. 在輸入數組上運行循環。
  2. 保持全局模式值和模式計數。
  3. 運行滑動窗口,直到找到相同的元素。
  4. 如果本地等元素數大於全局模式計數,則更新全局模式和模式計數。

這裏是工作和測試代碼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; 
}