Computer Algorithms

Introduction

In this sec­tion we are going to talk about algo­rithms in a wide sense, spec­i­fy­ing the char­ac­ter­is­tics, pseudocode, flow dia­grams and final­ly an exam­ple of prob­lem solv­ing, for­mu­lat­ing an algo­rithm, writ­ing the pseudocode, draw­ing the flow dia­gram and final­ly imple­ment­ing in C# in Uni­ty for the devel­op­ment of videogames.

What is an algorithm?

An algo­rithm is a finite series of ordered steps that describe the pas­sage from an ini­tial state to a final state of some process.

In gen­er­al we use algo­rithms to solve prob­lems, such as per­form­ing math­e­mat­i­cal cal­cu­la­tions or order­ing large lists of data.

Giv­en a spe­cif­ic prob­lem, the algo­rithm that solves it will be a finite series of sequen­tial steps that lead to the res­o­lu­tion of the problem.

Characteristics of an algorithm

An algo­rithm con­sists of a finite sequence of steps. 

Your instruc­tions must be well defined and unam­bigu­ous, i.e. each instruc­tion must be unam­bigu­ous­ly interpreted.

The instruc­tions of an algo­rithm must be ordered sequen­tial­ly, i.e. they are ordered instructions.

Pseudocode

The pseudocode is a col­lo­qui­al lan­guage descrip­tion of the algorithm.

In this case we give the algorithm's instruc­tions the form it will have in the code, but we don't use syn­tax of any par­tic­u­lar language.

Flowcharts and algorithms

Flow­charts are graph­i­cal rep­re­sen­ta­tions of algorithms.

Using a set of sym­bols and con­nect­ing with arrows you can describe the series of steps that an algo­rithm describes.

Implementation of a computational algorithm

For the pre­vi­ous steps we do not need to know any pro­gram­ming lan­guage. That is to say giv­en a con­crete prob­lem, to devise an algo­rithm that solves it and then to raise a dia­gram of flow to have a visu­al rep­re­sen­ta­tion, all this we can do with­out know­ing how to pro­gram and in some way we do it men­tal­ly every time we face the prob­lems in the dai­ly life.

To imple­ment the algo­rithm in a com­put­er pro­gram we must know a spe­cif­ic pro­gram­ming lan­guage and every­thing will depend on what we want to do.

Examples: Algorithms in Unity

Let's give an exam­ple of a prob­lem in the field of video game devel­op­ment with Unity.

Sup­pose we have a series of ene­mies sur­round­ing the play­er and we want to know which of them is the closest.

Presenting the problem

The objec­tive is to cre­ate a func­tion or method that, giv­en a set of GameOb­jects, returns the GameOb­ject that is clos­est to the player.

Algorithm

The sequence of steps to solve this prob­lem would be:

-Assume that the first ele­ment of the set is the one clos­est to the player.

-For items from posi­tion 2 onwards com­pare them with the clos­est object so far, if it is even clos­er to the play­er take it as the new object to com­pare. Repeat for all remain­ing items.

Pseudocode

pseudocodigo de un conjunto de  algoritmos que se encargan de encontrar todos los objetos cercanos al jugador, desarrollo de juegos en unity
Fig. 1:Pseudocode of an algo­rithm that, giv­en a set of objects, returns the clos­est object to anoth­er pre­de­fined object.

To write the pseudocode we take the algo­rithm and give it a pro­gram­ming struc­ture in col­lo­qui­al lan­guage, tak­ing into account the assign­ments, oper­a­tions, loops, if sen­tences and so on. 

Flowchart

The flow­chart will be a visu­al rep­re­sen­ta­tion of the pseudocode, using sym­bols pre­de­fined by con­ven­tion. Below we can see the flow­chart asso­ci­at­ed with the pre­vi­ous pseudocode.

diagrama de flujo para un conjunto de algoritmos que se encargan de encontrar todos los objetos cercanos al jugador, desarrollo de juegos en unity
Fig. 2: Flow­chart of an algo­rithm that, giv­en a set of objects, returns the clos­est object to anoth­er pre­de­fined object. 

Implementation of the algorithm in Unity

In this part we go to the con­crete appli­ca­tion of the algo­rithm, as we are going to use it for a func­tion in Uni­ty (videogame devel­op­ment) the imple­men­ta­tion will be in C# language.

funcion en programacion que implementa un algoritmo que se encarga de encontrar todos los objetos cercanos al jugador, desarrollo de juegos en unity
Fig. 3: Method in C# that imple­ments an algo­rithm that, giv­en a set of objects, returns the clos­est object to anoth­er pre­de­fined object. 

As we can see, the final result is a method in C# that solves the pro­posed problem.

Annexed article: What is a function in programming?

Scroll to Top
Secured By miniOrange