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.
Please Login to comment...