Part 1 - ALGORITHM: What is Algorithm in Computer Science
What is algorithm in computer science. Advantage of algorithm, Characteristics of the algorithm, Example of algorithm.
The Algorithm in Computer Science
Do you use software to make your task easier and smooth? But do you know, the software is nothing but a bunch of programs meant to solve the problems? And these programs are built upon the logical sequences, which are known as an Algorithm. Hence in this blog, we will discuss what an algorithm in programming is?
This blog is Part-I Algorithm: What is Algorithm in Programming and will be followed by another blog with the name Part II- Flowchart: What is Algorithm in Programming.
What is an Algorithm?
An Algorithm is a step-by-step series of instructions to perform a certain task or specified actions. Task can vary depending on the situation; it can be as simple as adding two numbers or as complex as applying artificial intelligence. Talk about any field, be it computer science, databases, networking, security, or operating system algorithms form the fundamental aspect of every field. Algorithms are created to simplify complex tasks into easy and effective steps.
What is an Algorithm in Programming and Coding?
The entire essence of programming and coding lies in the foundation of algorithms. They are written and created in the form of functions. Talking about functions, it is the way to define the relationship between values and variables. Programmers often create libraries: a collection of functions to save writing the same code again and again and extract whenever required.
Understand this way: In a nutshell, an algorithm transforms a finite input value, let say x, into an effective output value, let it be y, where y is f(x) for some well-defined function (f). And the best part is algorithms are generic and can be utilized in any programming language. They are the head chief who decide whether a problem can be solved or not; if yes how fast, how efficient and how accurate a solution can be created out of it.
Let’s say you want to go somewhere, and use the navigation app to get the route. That particular software will be built on code or programs. Their code consists of algorithms that instruct them on what to do. When we insert a destination, the software employs an algorithm to examine the numerous options. After that, it employs a different algorithm to check the current traffic, and then a third algorithm takes that data and generates the best possible path. Hence in this way, we get the final result.
Advantage of the Algorithm in programming
- It is a stepwise representation of solutions to a given problem, making it easy to understand.
- Every step in an algorithm has its logical sequence to be easy to debug.
- Using an algorithm, the problem is broken down into smaller pieces or steps; hence, it is easier for a programmer to convert it into an actual program.
- An algorithm acts as a program blueprint and helps during program development.
- An algorithm uses a definite procedure.
- It is easy first to develop an algorithm and then convert it into a flowchart and then into a computer program.
- It is not dependent on any programming language, so it is easy to understand for anyone, even without programming knowledge.
Types of Algorithm
Not to flood with so many technical algorithms name but to introduce to you some of the efficient algorithms of all times like merge sort, bubble sort, insertion sort, GCM Euclid’s algorithms, Greedy Algorithms, Brute Force Algorithm, Divide and Conquer Algorithms, Dynamic Programming, Hashing, Search Algorithms, String matching and Parsing, and Primality testing algorithms.
Characteristics of Algorithm
- Clear and Unambiguous: The Algorithm should be clear and unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning.
- Well-Defined Inputs: an algorithm should have well-defined inputs.
- Output: The Algorithm must clearly define what output will be yielded, and it should be well-defined as well.
- Finite: The Algorithm must be finite, i.e. it should not end up in infinite loops or similar.
- Feasible: The Algorithm must be simple, generic, and practical, such that it can be executed upon use.
- Language Independent: it should be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be the same, as expected.
How to Design an Algorithm?
The pre-requisites required for writing an algorithm are as follows.
- Define the problem by conducting a thorough analysis of the situation.
- Constraints should be studied properly
- Input parameters to write an algorithm
- The expected output of the problem
- Solution of the problem
Step-by-Step Designing of an Algorithm
Step 1: Fulfilling the pre-requisites
- As discussed above, to write an algorithm, its pre-requisites must be fulfilled.
- The problem is to be solved by this algorithm: Add three numbers and print their sum.
- The constraints of the problem that problem considered while solving the problem: The numbers must contain only digits and no other characters.
- The input to be taken to solve the problem: The three numbers to be added.
- The output to be expected when the problem is solved: The sum of the three numbers taken problem input.
- The solution to this problem, in the given constraints: The solution consists of adding the three numbers. It can be done with the help of the ‘+’ operator, bit-wise or any Algorithmhod.
Step 2: Designing the Algorithm
Now let’s design the algorithm with the help of the above pre-requisites:
Algorithm to add three numbers and print their sum:
- START
- Declare three integer variables num1, num2 and num3.
- Take the three numbers to be added as inputs in variables num1, num2, and num3, respectively.
- Declare an integer variable sum to store the resultant sum of the three numbers.
- Add the three numbers and store the result in the variable sum.
- Print the value of the variable sum.
- END
Step 3: Testing the Algorithm by implementing it.
To test the algorithm, let's implement it in C# language.
The output of the above program will come as follows:
Enter the 1st number: 10
Enter the 2nd number: 12
Enter the 3rd number: 3
The sum of the three numbers is: 25
Hence, we come to the end of PART 1: ALGORITHM where we provided a brief overview of the Algorithm in programming.
The second part of the presentation can be viewed by clicking this link: where we explain PART 2: FLOWCHART.