Algorithm Expressions
Algorithms can be expressed in various notations, including natural language, flowcharts, pseudocode, and programming languages. Expressing algorithms in natural language tends to be wordy and ambiguous, making it unsuitable for complex algorithmic structures. Instead, flowcharts and pseudocode offer more structured and standardized approaches to describe algorithms clearly while remaining independent of any specific programming language.
Programming languages are used to express algorithms as executable code that can be run by a computer. In communities or teams that primarily use a single programming language, algorithms may be written in a way that closely resembles that language for the sake of familiarity and efficiency.
The algorithms used to solve problems can vary significantly, depending on the developer’s understanding and logic. It’s important to note that a good algorithm is not just one that works—it’s one that also optimizes time and resource usage.
Let’s compare two sample algorithms for finding the largest of three numbers. One is written in natural language, and the other uses pseudocode.
Algorithm in Natural Language:
-
Start
-
Input number1, number2, and number3
-
Assume the largest number is number1
-
If number1 is greater than the current largest, update the largest to number1
-
If not, check if number2 is greater than the current largest. If yes, set the largest to number2
-
If not, the largest is number3
-
End
Algorithm in Pseudocode:
-
Start
-
Input number1
-
Input number2
-
Input number3
-
Set largest ← number1
-
If number1 > largest then7. largest ← number1
-
If number2 > largest then9. largest ← number2
-
Else11. largest ← number3
-
Display "The largest number is", largest
-
End
When comparing the natural language and pseudocode versions, pseudocode is clearly easier to understand. It provides better structure and avoids ambiguity, allowing for more efficient translation into programming code. Here's a brief snippet of pseudocode to illustrate how indentation and logical structure work:
...If number1 > largest thenlargest ← number1If number2 > largest then...
The indented line (largest ← number1) signifies that this operation only executes if the condition above it (number1 > largest) is true. If not, the algorithm continues with the next condition (number2 > largest), and so on.
Translating Pseudocode into Java
The pseudocode can easily be converted into a programming language such as Java. Here's what that would look like:
// Program Class Namepublic class LargestNumber {public static void main(String[] args) {int number1 = 10;int number2 = 15;int number3 = 5;int largest = number1;if (number1 > largest)largest = number1;else if (number2 > largest)largest = number2;elselargest = number3;System.out.println("The largest number is = " + largest);}}
Why Is This Transformation Possible?
The transformation from algorithm to programming code is possible because experienced analysts and programmers have a strong instinct for interpreting algorithms. They use logical thinking to structure solutions, making it natural to move from abstract steps to actual code.

No comments:
Post a Comment