An Introduction to Algorithms in Computer Science
Tags: programming, codenewbie, computerscience, algorithms
Published: 2021-11-18 11:22:13

## Table of Content

-  [What are Algorithms and why are they so important?](#what-are-algorithms-and-why-are-they-so-important)
-   [Picking the right algorithm: What makes a good algorithm?](#picking-the-right-algorithm-what-makes-a-good-algorithm) 
-  [Correctness vs Efficiency](#correctness-vs-efficiency) 
-   [How to measure the efficiency of an algorithm?](#how-to-measure-the-efficiency-of-an-algorithm) 
-  [Conclusion](#conclusion) 



## What are Algorithms and why are they so important?

An algorithm is a specific procedure for solving a well-defined computational problem. In simpler terms, we can say that an algorithm is a set of step-by-step instructions or finite processes which if followed, solve a specific problem. Algorithms are useful in analyzing huge data sets or selecting intelligently from a vast number of possible options. They allow massive computational power to be harnessed into meeting people’s needs and solving problems.

Let’s talk about a few applications of algorithms;

Say, for example, you wanted to buy groceries at the nearest grocery store, two streets away from your house. You could write an algorithm to perform this task as thus;


- Write a grocery list.
- Leave the house.
- Drive to the store.
- Pick up a grocery basket.
- Go to the aisle where every item you have on your list is and put them in the basket.
- Go to the checkout point to pay for your items.
- Drive back home.

What about an algorithm for making tea?

In this order, you’d want to;


- Boil water. If using an electric kettle with temperature setting, set it to about 208°F for black tea. 
- Warm-up teapot.
- Put the tea into a teapot and add hot water.
- Cover teapot and steep tea for 5 minutes.
- Strain tea solids and pour hot tea into teacups.

There, you have it!

You have successfully written out a set of step-by-step instructions to carry out two different tasks.  

Let’s take a few practical examples. 

Have you ever wondered how Zoom transmits live videos between people in different parts of the world? If you guessed audio and video compression algorithms, then you’re correct!

Google Maps uses route-finding algorithms to help you locate the shortest route to places you want to go. Twitter curates your feed by monitoring your engagement through optimization algorithms. These and so on are practical applications of algorithms in building scalable and reliable software and services that make our everyday lives, quite interesting.

While of course, algorithms in Computer Science are more complex than our day-to-day applications of algorithms like grocery shopping or starting up your car, they all boil down to the same process of following steps of instructions to solve problems.


## Picking the right algorithm: What makes a good algorithm?

So how do you know what algorithm to pick?

if you’ll be picking an existing algorithm or writing a new one, it is important to apply one that saves you time and effort. It is quite helpful to know how to design algorithms and to analyze them for correctness and efficiency.

Now, not all algorithms are made equal. Two things qualify a good algorithm;


- **Correctness** - If you’re going to be writing an algorithm to carry out a specific task. You want to be sure it can carry out that task error-free. A good algorithm carries out the task at hand, accurately.


- **Efficiency** - Asides from carrying out the task correctly, you’d also want an algorithm that would do so efficiently as well, saving us space and most importantly, time. A good algorithm not only performs a task correctly but efficiently too.


## Correctness vs Efficiency

Most of the time, you want to pick an algorithm based on its ability to solve problems correctly or produce correct answers only. Other times, you could live with an algorithm that doesn’t give you the best answer because the one that does, takes a really long time to run. 

Take for example;

Say we want to write a program to find the most efficient route to your favorite cloth store, after delivering milk to 30 different locations. It would take days or even weeks to run! But you could settle for an algorithm that finds a good route, maybe not the best, and it would run in seconds.

Let’s re-examine our grocery shopping example again;

While of course, the previous algorithm helps you perform your grocery shopping task quite correctly, it could be better. You could go with the online shopping option. Then our algorithm will look like this;


- Go to the store’s website.
- Find the item you want using their search option.
- Add all the items on your list to your cart. 
- Proceed to checkout to pay for your goods.
- Get your goods delivered to your doorstep.

In this case, you get to perform the same task without leaving your bed, saving you both time and energy which could have been spent driving back and forth and walking around the supermarket, searching manually for the items on your list.

So it is in Computer Science, we constantly seek ways to perform a task in the least time, consuming the lowest storage possible. Sometimes, complete accuracy is a must so we have to use a certain algorithm no matter how long it takes to run. Other times, good is good enough and saves us a lot of time and unnecessary stress.


## How to measure the efficiency of an algorithm?

You could time how long it takes an algorithm to run but that would only tell us about that particular implementation in a certain programming language on a particular local machine. That is to say;

The actual running time depends on factors like:


- The speed of the computer.
- The compiler and compiler options.
- The quantity of data.
- Programming language used (Lower-level languages like C++ run faster than Higher-level languages like Java).
- Available memory size.
- Algorithm time complexity (we’ll get to this later in this series).

Hence the measure of the amount of time for an algorithm to execute, which is the Time Efficiency of the algorithm, due to the variable factors on which it is dependent, is not enough metric to conclude the overall efficiency of an algorithm.

What about space?

Earlier in this article, we learned that efficiency is measured in relation to time and space. We could also measure the Space Efficiency, which is a measure of how much memory an algorithm uses in its execution, but it still isn’t the most reliable way to measure the overall efficiency of the algorithm.

How then do we measure the efficiency of an algorithm?

One technique scientists employ is called **Asymptotic Analysis**. This technique allows algorithms to be compared independently of programming language or hardware so that we can conclude correctly what algorithms are more efficient than the others.

In the next article, we’ll be looking at Asymptotic Analysis and how it can help us analyze and choose the right algorithm for a specific task.


## Conclusion

In this article, we discussed the basics of algorithms and how they work, the two criteria for picking an algorithm, and the dilemma of picking one criterion over the other. In the next article, we’ll be discussing Asymptotic Analysis and how this technique helps us determine the efficiency of an algorithm and its suitability for the task at hand.

See you there!🎉🎉