Basic Algorithm Structures

Basic Algorithm Structures

When building a house, the first thing to consider is the foundational construction, such as the foundation, concrete beams, and wall structure. The same concept applies when building an algorithm—you must understand its basic structure. There are three fundamental structures of algorithms: sequential, selection, and looping.

Sequential

The sequential structure is where an algorithm is built with steps (instructions/commands) that are executed in order, without skipping any. For instance, if an algorithm has 20 steps, all must be performed sequentially from step 1 to step 20 without omission.

This sequential logic also applies to programming languages. When programming instructions are written and processed by a computer, the computer will process and interpret these instructions line-by-line in order, from the first line to the last.

With this structure, algorithm developers must analyze and determine which instructions should come first, which follow, and which are last.

The diagram below illustrates a sequential algorithm for calculating speed and acceleration. No step is skipped or executed out of order. For example, the calculation of acceleration (a) does not precede the calculation of speed (v) because speed must be calculated first to obtain acceleration.

Basic Algorithm Structures Sequential Flowchart

If this sequential structure is represented in pseudocode, the task order remains the same. See the pseudocode example below for calculating speed and acceleration:

Basic Algorithm Structures Sequential Pseudocode

Selection

In reality, many algorithms contain selection processes within their bodies. Selection instructions are used when there are two or more possible alternatives or decisions to be made.

Basic Algorithm Structures Selection Flowchart

The image above shows an algorithm with a two-decision selection structure. Two numbers are compared to determine which is greater, equal, or smaller. For example, if a = 6 and b = 5, the output will be “Value a >= b”. If the opposite is true, the output will be “Value a < b”.

Basic Algorithm Structures Selection Pesudocode

In algorithm design and programming, such selection cases frequently arise, either with similar or different conditions.

Looping

The third basic structure is looping. Many real-world processes are repetitive. For example, memorizing something often involves repeating the material taught by a teacher or lecturer. Repetition can be an effective way to understand or memorize.

Loops are essential in algorithms to solve many problems. Sorting large amounts of data in programs is made possible by looping algorithms. Imagine having to manually write numbers from 1 to 100 in a program—it would be time-consuming. However, with a looping algorithm, only four lines of code may be needed to display numbers from 1 to 100 or more.

Have you ever used face or fingerprint recognition features on your smartphone? These AI technologies heavily utilize looping algorithms.

Previously, we showed how to handle repetition using algorithm instructions. With proper looping instructions, the repetition becomes much easier and more practical than writing, for example, numbers 1 to 5 manually, like this:

Basic Algorithm Structures Looping Flowchar

At first glance, this may seem fine because the number of printed values is small. But imagine printing numbers from 1 to 100 with that same pattern—you'd need 102 flowchart symbols (including start and end), making the flowchart highly inefficient.

So, is there a more efficient way? Yes—by using a looping algorithm. Below is an example of a flowchart that prints numbers from 1 to 100 by combining a loop with a selection structure:

Basic Algorithm Structures Looping Flowchart

Now compare the above diagram to the previous one. The previous version could only print 1 to 5. If you wanted to print more, you'd have to add new symbols. But with the newer version, you can print up to 100—or even 1,000—just by changing b = 1000. Efficient, right?

Basic Algorithm Structures Looping Pseudocode

How does the loop process work in a flowchart or pseudocode? Here’s an explanation:

Initially, variable a is set to 1 and b is set to 100. (First Loop) Then, a is updated with a = a + 1, so a = 1 (since a = 0 + 1). Next, a is compared with b: is a > b or 1 > 100? Of course not, so the result is “False”, and variable a is printed. (Second Loop) Then it returns to a = a + 1 or a = 1 + 1, so a = 2. This process continues, printing numbers up to 100 until the condition a > b is true (i.e., “Yes”), and then the loop ends.


References
KANA, ALGORITHM and PROGRAMMING, 2024

No comments:

Post a Comment