This article provides a concise introduction to computer algorithms, specifically focusing on Python, and includes an example of an algorithm to find the maximum number in a list.
Abstract
The article begins by explaining that when solving a problem with a computer, the first thing to consider is the solution algorithm. It defines an algorithm as an ordered set of defined steps to solve a problem and obtain a logical result within a finite amount of time. The article then compares algorithms and programs, stating that an algorithm is a problem solution description, while a program is the actual implementation of that solution using a programming language. The article concludes with an example of an algorithm to find the maximum number in a list.
Bullet points
An algorithm is an ordered set of defined steps to solve a problem and obtain a logical result within a finite amount of time.
Algorithms can be formulated by defining inputs, outputs, and steps that map the inputs into the desired outputs.
Multiple algorithms can be used to solve the same problem, and they can be compared based on factors such as execution time.
An algorithm is a problem solution description, while a program is the actual implementation of that solution using a programming language.
The article includes an example of an algorithm to find the maximum number in a list.
A Concise Intro to Computer Algorithms: Python Complete Course — Part 2
· This article is a part of the Python Complete Beginner to Expert Course
which you can find it here.
· This article is also available as a YouTube video here.
Introduction
The first thing that you have to think about when you want to solve a problem with a computer is the solution algorithm. So, what is an algorithm? and how it is different from a computer program?
To answer these questions, this article will cover the following Outlines:
Usually, when we have a problem and we want to solve this problem using computers, the first thing to think about is the solution algorithm.
The algorithm is an ordered set of defined steps to solve a problem and obtain a logical result within a finite amount of time, From this definition, we can understand that the algorithm represents the steps that should be implemented inside the program in order to solve the problem.
Generally, We formulate the algorithm by defining the (inputs, outputs, and steps) that map the inputs into the desired outputs. Refer to Figure 1.
Figure 1: An algorithm formulation (Image By Author)
Also, it is a very important point to know that one problem could be solved using multiple algorithms and over time you will get the required experience to know the best algorithm quickly. Usually, we compare algorithms using different factors such as execution time. For example, we say one algorithm is better than another if they achieve the same target but it requires less amount of execution time.
Next, let us compare the algorithm and the program and find the key differences between them.
After we understand the meaning of the algorithm, now we should be able to discriminate between the algorithm and the program.
Algorithm: It is just a problem solution description in which we define (inputs, outputs, and steps).
Program: It is the actual implementation of that solution using one of the programming languages.
As a result, the algorithm represents the solution, whereas the program represents the implementation.
Now, to understand well what an algorithm is, let us see a concrete example.
3. An Algorithm Example
To understand the idea better let’s take a complete example of an algorithm.
Assume you have the following list [1,5,3,4] and you want to know the maximum number inside this list.
You, as a human, will directly say that the maximum number is 5.
It’s a very easy problem for you because this list has only four items, but what if this list has 1 million items?
It’s very hard to be solved directly by you as a human, and you have to use a computer. So, you have to start to think in the way that the computer thinks.
In other words, you have to set a solution algorithm in which you have to define inputs, outputs, and steps.
In this case:
Inputs: are a list of positive numbers, and let’s call this list (L).
Outputs: in this case, we have a single output which is the largest number in this list, and let’s call this largest number(n).
Algorithm Steps
Set the initial value of (n) to be zero.
For each number(x) inside the list, compare this number with the largest number which is (n).
If you find that (x) is larger than (n), just update the value of (n) by the value of (x).
Let’s explain these steps visually:
First of all, you have the following list which has [1,5,3,4] and you will set the initial value of (n) to be (0). Refer to Figure 2.
Figure 2: Setting the initial value of (n) to be (0) (Image By Author).
Now I will compare the first item which is x=1 with (n), and it’s clear that (1) is larger than (0) so I will update the value of (n) to be equal to (x) which is equal to (1). Refer to Figure 3.
Figure 3: Comparing the first item with n (Image By Author).
Do the same comparison for (5) and you will find that (5) is bigger than (n) which is (1). So, update the value of (n) with (5). Refer to Figure 4.
Figure 4: Comparing the second item with n (Image By Author).
If you do the same comparison for (3) and (4), you will find that (x) is smaller than (n) in both cases and the value of (n) will not be updated, and we will keep the value of n to be n=5.
Finally, you will reach the final item which is (4) and there there are no more comparisons required, and the variable (n) will have the largest number which is (5). Refer to Figure 5.
Figure 5: finish comparisons (Image By Author).
Now, let us summarize what we have learned in this article:
P.S.: A million thanks for your time reading my story. Before you leave let me mention quickly two points:
First, to get my posts in your inbox directly, would you please subscribe here, and you can follow me here.
Second, writers made thousands of $$ on Medium. To get unlimited access to Medium stories and start earning, sign up now for Medium membershipwhichonly costs $5 per month. By signing up with this link, you can directly support me at no extra cost to you.