Algorithims

A algorithm is a structured sequence of steps which, when given a input, will produce a output. Both the input and output are well defined. What we mean by “well defined” is that it is predictable and already validated. If it helps you can think of a algorithm a bit like a function. It has a number of inputs (parameters) and a output (the return).

Algorithms are only concerned with the processing of the input to produce the correct output. A serial search, which is a well known and simple algorithm, has a well defined input and output. The input is the list of values and the item you are searching for. The output is the position the item is located at.

Input – List[], searchTerm
positionOfSearchTerm = -1
for a=0 to list.length
            if searchTerm == List[a] then positionOfSearchTerm = a
Next
Output – postitionOfSearchTerm

The above algorithm is written in psuedocode rather than Java. We will cover psuedocode in more detail later. We can write it in English if required -

Input – List[], searchTerm

  1. set positionOfSearchTerm to be -1
  2. start at the first position of List
  3. compare the current position of List to the searchTerm
  4. if equal then set positionOfSearchTerm to be the current position
  5. move onto the next position and repeat the comparision until the end of the list has been reached.

Output – postitionOfSearchTerm

It also can be written as a flow chart -