What is an Algorithm?
An algorithm is a finite sequence of well-defined, step-by-step
instructions or rules designed to perform a specific task or solve a
particular problem. It is a systematic method for solving a problem or
achieving a goal. An algorithm takes an input, processes it, and produces
an output after a series of steps.
Algorithms are fundamental to computer science and are used in software
development, data processing, problem-solving, and decision-making.
Characteristics of an Algorithm
A good algorithm should have the following characteristics:
1. Finiteness: The algorithm must terminate after a finite number of
steps. It cannot run indefinitely.
2. Definiteness (Clarity): Each step in the algorithm must be
precisely defined and unambiguous. Every instruction should be
clear and understandable.
3. Input: An algorithm must take zero or more inputs. Inputs are the
data provided to the algorithm for processing.
4. Output: An algorithm must produce at least one output. This output
is the result of the algorithm’s execution.
5. Effectiveness: The steps of the algorithm must be basic enough to
be carried out, in principle, by a human with a pencil and paper. The
steps should be simple and feasible.
6. Generality: An algorithm should be applicable to a broad set of
problems, not just a single instance. It should be general enough to
handle different input values.
Example: Algorithm to Find the Maximum Element in an Array
Let’s consider a simple example: an algorithm to find the maximum
number in a given list of numbers. Here's how we can define it:
Problem:
Given an array of numbers, find the maximum number in the array.
Algorithm:
1. Start with the first element of the array as the current maximum.
2. Traverse through each element of the array, comparing it with the
current maximum.
3. If an element is greater than the current maximum, update the
current maximum.
4. After checking all elements, the current maximum will be the largest
number in the array.
5. Return the current maximum as the result.
Pseudocode:
Algorithm FindMax(arr):
max = arr[0] // Step 1: Initialize the maximum value
for each element e in arr[1..n-1]: // Step 2: Traverse the array
if e > max: // Step 3: Compare each element with the current max
max = e // Step 3: Update the maximum if necessary
return max // Step 4: Return the maximum element
Characteristics in Action:
1. Finiteness: The algorithm terminates after examining each
element in the array exactly once, so it will always finish in a finite
number of steps.
2. Definiteness: Each step is clearly defined: first, initialize the
maximum; then, iterate over each element and compare it with the
current maximum; finally, return the maximum found.
3. Input: The algorithm takes an array arr as input, which is the list of
numbers.
4. Output: The algorithm outputs the largest number from the array.
5. Effectiveness: Each step is simple and feasible to perform. For
example, comparing two numbers is a basic operation that can be
done manually.
6. Generality: This algorithm works for any array of numbers, whether
the array is small, large, positive, negative, or contains repeated
elements.
Example Execution:
Suppose the input array is:
arr = [3, 5, 7, 2, 6]
Following the steps of the algorithm:
1. Start with max = 3.
2. Compare 5 with max (3). Since 5 > 3, update max = 5.
3. Compare 7 with max (5). Since 7 > 5, update max = 7.
4. Compare 2 with max (7). No change, so max = 7.
5. Compare 6 with max (7). No change, so max = 7.
Finally, the algorithm returns 7 as the maximum value.
Conclusion:
An algorithm is a well-defined, step-by-step procedure for solving a
problem. Its characteristics ensure that the algorithm is clear, effective,
and efficient for a wide range of inputs. In the example above, we showed
how an algorithm to find the maximum value in an array satisfies the key
characteristics.