How to Use Algorithms to Solve Problems?
Last Updated :
09 Aug, 2021
An algorithm is a process or set of rules which must be followed to complete a particular task. This is basically the step-by-step procedure to complete any task. All the tasks are followed a particular algorithm, from making a cup of tea to make high scalable software. This is the way to divide a task into several parts. If we draw an algorithm to complete a task then the task will be easier to complete.
The algorithm is used for,
- To develop a framework for instructing computers.
- Introduced notation of basic functions to perform basic tasks.
- For defining and describing a big problem in small parts, so that it is very easy to execute.
Characteristics of Algorithm
- An algorithm should be defined clearly.
- An algorithm should produce at least one output.
- An algorithm should have zero or more inputs.
- An algorithm should be executed and finished in finite number of steps.
- An algorithm should be basic and easy to perform.
- Each step started with a specific indentation like, "Step-1",
- There must be "Start" as the first step and "End" as the last step of the algorithm.
Let's take an example to make a cup of tea,
Step 1: Start
Step 2: Take some water in a bowl.
Step 3: Put the water on a gas burner.
Step 4: Turn on the gas burner
Step 5: Wait for some time until the water is boiled.
Step 6: Add some tea leaves to the water according to the requirement.
Step 7: Then again wait for some time until the water is getting colorful as tea.
Step 8: Then add some sugar according to taste.
Step 9: Again wait for some time until the sugar is melted.
Step 10: Turn off the gas burner and serve the tea in cups with biscuits.
Step 11: End
Here is an algorithm for making a cup of tea. This is the same for computer science problems.
There are some basics steps to make an algorithm:
- Start - Start the algorithm
- Input - Take the input for values in which the algorithm will execute.
- Conditions - Perform some conditions on the inputs to get the desired output.
- Output - Printing the outputs.
- End - End the execution.
Let's take some examples of algorithms for computer science problems.
Example 1. Swap two numbers with a third variable
Step 1: Start
Step 2: Take 2 numbers as input.
Step 3: Declare another variable as "temp".
Step 4: Store the first variable to "temp".
Step 5: Store the second variable to the First variable.
Step 6: Store the "temp" variable to the 2nd variable.
Step 7: Print the First and second variables.
Step 8: End
Example 2. Find the area of a rectangle
Step 1: Start
Step 2: Take the Height and Width of the rectangle as input.
Step 3: Declare a variable as "area"
Step 4: Multiply Height and Width
Step 5: Store the multiplication to "Area", (its look like area = Height x Width)
Step 6: Print "area";
Step 7: End
Example 3. Find the greatest between 3 numbers.
Step 1: Start
Step 2: Take 3 numbers as input, say A, B, and C.
Step 3: Check if(A>B and A>C)
Step 4: Then A is greater
Step 5: Print A
Step 6: Else
Step 7: Check if(B>A and B>C)
Step 8: Then B is greater
Step 9: Print B
Step 10: Else C is greater
Step 11: Print C
Step 12: End
Advantages of Algorithm
- An algorithm uses a definite procedure.
- It is easy to understand because it is a step-by-step definition.
- The algorithm is easy to debug if there is any error happens.
- It is not dependent on any programming language
- It is easier for a programmer to convert it into an actual program because the algorithm divides a problem into smaller parts.
Disadvantages of Algorithms
- An algorithm is Time-consuming, there is specific time complexity for different algorithms.
- Large tasks are difficult to solve in Algorithms because the time complexity may be higher, so programmers have to find a good efficient way to solve that task.
- Looping and branching are difficult to define in algorithms.
Similar Reads
How to improve your DSA skills? Enhancing data structure and algorithm skills allows for achieving proficiency in programming and excelling in technical interviews. These foundational skills enable efficient problem-solving and algorithm design, fostering success in the competitive world of software development and job interviews.
4 min read
Definition, Types, Complexity and Examples of Algorithm An algorithm is a well-defined sequential computational technique that accepts a value or a collection of values as input and produces the output(s) needed to solve a problem. Or we can say that an algorithm is said to be accurate if and only if it stops with the proper output for each input instanc
13 min read
Interesting Examples of algorithms in everyday life Ever found shortest path from Place A to Place B on Google Maps? Ever rolled a dice just by a click in an online game? Ever used search functionality in a website? One thing which is common to all these scenarios is that one or other algorithm is being run and results are being delivered. Simply sta
2 min read
Root Finding Algorithm Root-finding algorithms are tools used in mathematics and computer science to locate the solutions, or "roots," of equations. These algorithms help us find solutions to equations where the function equals zero. For example, if we have an equation like f(x) = 0, a root-finding algorithm will help us
8 min read
Data Structures & Algorithms (DSA) Guide for Google Tech interviews Google is known for its rigorous and highly competitive technical interviews. These interviews are designed to assess a candidate's problem-solving abilities, technical knowledge, and cultural fit with the company. Preparing for technical interviews at top companies like Google requires a solid unde
9 min read
Introduction to Optimization with Genetic Algorithm Optimization is the process of finding the best solution after evaluating all possible combinations. When dealing with complex problems, finding the optimal solution becomes crucial. One powerful tool in machine learning for solving such optimization problems is the genetic algorithm. Inspired by th
10 min read