CodeNewbie Community 🌱

Bam_boogie
Bam_boogie

Posted on

Algorithms for a newbie

Before I began learning to code, I often encountered the word algorithm in various media. Be it movies, video games or even cartoons, the word was thrown around whenever a smart person was speaking! My understanding of what an algorithm was was vague and along the line of, β€œit’s a bunch of code that makes the thing do things.” To be honest, that description is not too far from the truth. An algorithm consists of lines of code, which work as instructions for the computer to execute a task. An algorithm can be defined as a finite sequence of instructions, which can be formed in a finite amount of time and effort. When writing an algorithm to solve a particular problem, we ask a few questions:

  1. What is the algorithm supposed to do? (Also known as specification.)
  2. Does it do what it is supposed to do? (Also known as verification.)
  3. How efficiently does it do it? (Also known as performance analysis.)

Specification consists of formalizing the crucial details of the problem that the algorithm is intended to solve. Typically, you will have to specify how the inputs and outputs of the algorithm are related. For simple problems, such as verifying user authentication, it is easier to tell whether an algorithm satisfies its specification. But all algorithms are not built the same; in more complex cases, more time must be spent on answering the second question (verification).

Verifying an algorithm involves creating a series of test cases which will put the algorithm to work and validate its level of correctness. This begs the question, how many test cases will one algorithm require? In theory, an algorithm could have an infinite amount of test cases, I believe it comes back to understanding the purpose of your algorithm (step one) that will help you create a good variety of cases. There is a lot more to verification which I can’t cover in this post (I’m still learning) but testing is a very important component of algorithms!

Finally, the efficiency or performance of an algorithm relates to the resources required by it, such as how quickly it will run, or how much computer memory it will use. This will usually depend on the problem instance size, the choice of data representation, and the details of the algorithm. To measure the efficiency of an algorithm, software engineers use a concept known as Big O Notation. Big O Notation measures the time it takes to run your function as the input grows. Or in other words, how well does the function scale.

Top comments (0)