The 5 basic algorithms in programming for beginners

We call basic algorithms to the algorithms that you can use to solve many common problems in programming. In this article, I show you how you can use these basic algorithms in many different situations.

The 5 basic algorithms in programming that every beginner should learn are: counting, summing, minimum, maximum and searching. Once you learn this algorithms you will have a good arsenal that you will be able to use many times to solve different problems.

Table of Contents

Algorithms in programming

Algorithms are the most important topic to learn if you want to be a great programmer. It is my recommended way to learn how to code. Once you master algorithm topics, you can be sure you are going to be a successful programmer.

Computer science degrees are based on algorithms. In those degrees, you will learn algorithms directly and indirectly. One way to become very good in algorithms is through mathematics knowledge. 

Another way is to study specific algorithms topics. Design and Analysis of Algorithms is one of the main topics in computer science, and it is a compulsory subject for computer science students.

You can get the best book on the topic below. It is the main textbook that lecturers are using, and for me, it is a regular consultation material that is always close by me.

Basic algorithms

I call basic algorithms to simple algorithms that we use most of the time while coding, and sometimes we do not even realise that we are using them.

Notice that “basic algorithms” is not the same than “the most used algorithms for a specific topic”. For instance, the most used algorithms for searching, or sorting, etc.

We use the basic algorithms most of the time when we work with arrays or collections.

You can solve most of the problems related to the design of an algorithm using an array data structure using a few algorithms as the basis. 

In this article, I show you what I consider are the basic algorithms and a few examples of why.

Notice that the reader must have basic knowledge of arrays or collections to understand these basic algorithms.

Counting basic algorithm

We can use this basic algorithm to count elements from a collection that meet a condition.

Find below some simple problems that you can solve using this basic algorithm.

  • How many students passed the programming test?
  • What is the number of foreigner students in my class? How many are female?
  • How many MacBook Pro do we have in stock?
  • Number of computers that were sold last week?
  • How many players from Brazil have a specific soccer team?

As you can see by now, there are many different questions/problems that we can find out there. So, how can we solve the previous problems from a programming point of view?

The answer is easy. We can solve all the previous problems/questions using the counting basic algorithm. All we have to do is to choose the right condition (see line 3 below) 

Basic counting algorithm in programming

We use the counting algorithm to solve many complex problems in programming (i.e. in graph theory, artificial intelligence among others).

Counting basic algorithm

Summing basic algorithm

Another basic algorithm that we can use in many situations is the sum basic algorithm.

Let’s see a sample questions/problems that we can answer/solve with this basic algorithm.

  • How much money can we have after we sell all our products?
  • What is the average income in the country?
  • How much money does a company need, to pay all the employees?
  • What amount of traffic does a website get in a year?
  • How much disk space we need to make a backup copy of five computers?

The answer to the previous (and much more) questions lays in the sum basic algorithm.

Basic summing algorithm in programming

Notice that in this algorithm, we add to s the values (see line 4). It is not the same than adding 1 (see line 4 -count algorithm) like in the counting algorithm.

Summing basic algorithm
Summing basic algorithm

Minimum and Maximum basic algorithm

In several situations, we will want to know the minimum or maximum value in a collection. Below are some of the examples:

  • Who won the race competition?
  • What student has the best overall results?
  • Who is the best-paid employee in my company?
  • What sales representative brought the less amount of sales last month?
  • Which country has the biggest GDP?
  • What bank offers the best competitive rates? 
Basic minimum and maximum  algorithm in programming

You can answer all the previous questions using this basic algorithm. If you are searching for a maximum (the student with the best results) you can use lines 4 to 6 and remove lines 7 to 9. On the other hand, if you are searching for a minimum, you can remove lines 5 to 6.

Searching basic algorithm

The last basic algorithm I show you in this article is the search basic algorithm. 

In this case, I want to specify that there are several search algorithms, that are more efficient than the one I’m going to show you here. However, you cannot use those algorithms all the time because they depend on the knowledge about the input. 

For instance, are the elements of the collection in order? Is the structure that we are using a tree?

I’m showing you here just the basics when we only now that we can compare the elements of the collection.

Some of the related question/problems are:

  • Is there an employee older than 50?
  • Do we have a MacBook Pro in stock?
  • Is the domain “basicalgorithms.com” available?
  • Is this person one of our customers?
  • Do we have the book “Introduction to Algorithms”? 
Basic searching algorithm in programming

Conclusions

I showed you the basic algorithms and several situations that can be solved using these algorithms.

As you could see, you can benefit from using basic algorithms in programming. You can use them in several situations.

That is why I call them basic algorithms in programming. You will have to use them most of the times, whether you know them as basic algorithms or not.

If you enjoyed the reading, leave a comment below.

This post may contain affiliate links to sustain our blogging endeavours. If you use these links to buy something we may earn a commission at no extra cost for you. We only recommend what we already used ourselves and got a positive result.