Mode

PROBLEM
The problem is to determine the most frequently occuring value, called the mode, of a sorted array.

ALGORITHM
The elegant algorithm used to compute the mode was invented by Griffits.

IMPLEMENTATION
Unit: internal function
 
Global variables: the input sorted array A. of arbitrary elements
 
Parameter: a positive integers N - number of elements in A.
 
Returns: Couple of values Mode and Frequence of mode separated by blanks.
 

MODE: procedure expose A.
parse arg N
Mode = A.1; Frequence = 1
do J = 2 to N
  JmFrequence = J - Frequence
  if A.J = A.JmFrequence
    then do
      Frequence = Frequence + 1; Mode = A.J
    end
end
return Mode Frequence

 

EXAMPLE

 

N = 5
A.1 = 1; A.2 = 2; A.3 = 2; A.4 = 3; A.5 = 5
say MODE(N)
exit
MODE: procedure expose A.
...

 

displays 2 2

 

Literature
Gehani N., Ada - An Advanced Introduction
Prentice-Hall, Inc. Englewood Cliffs, New Jersey 1983


Cover Contents Index Main page

last modified 4th August 2001
Copyright 2000-2001 Vladimir Zabrodsky
Czech Republic