Thursday, July 30, 2009

Algorithm

Algorithm is a finite set of instructions which, if followed, will accomplish a task. It has five important properties: finiteness, definiteness, input, output and effectiveness. Finiteness means an algorithm must terminate after a finite number of steps. Definiteness is ensured if every step Justify Fullof an algorithm is precisely defined. For example, "divide by a number x" is not sufficient. The number x must be define precisely, say a positive integer. Input is the domain of the algorithm which could be zero or more quantities. Output is the set of one or more resulting quantities which is also called the range of the algorithm. Effectiveness is ensured if all the operations in the algorithm are sufficiently basic that they can, in principle, be done exactly and in finite time by a person using paper and pen.

Java Tutorial Sour Code: Algorithm

Consider the following example:

public class Minimum {

public static void main(String[] args) {
int a[] = { 23, 45, 71, 12, 87, 66, 20, 33, 15, 69 };
int min = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] < min) min = a[i];
}
System.out.println("The minimum value is: " + min);
}
}

Java Tutorial Sour Code: Algorithm

The Java code above returns the minimum value from an array of integers. There is no user input since the data from where to get the minimum is already in the program. That is, for the input and output properties. Each step in the program is precisely defined. Hence, it is definite. The declaration, the for loop and the statement to output will all take a finite time to execute. Thus, the finiteness property is satisfied. And when run, it returns the minimum among the values in the array so it is said to be effective.

Java Tutorial Sour Code: Algorithm

All the properties of an algorithm must be ensured in writing an algorithm.

No comments:

Post a Comment