Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1.

3 Algorithms

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.

CREATING AN ALGORITHM:

Algorithm is language-independent. It is necessary to write the steps for demonstrate the logic behind the solution to be
used for solving a problem.

 The algorithm should be clear and unambiguous.


 There should be 0 or more well-defined inputs in an algorithm.
 An algorithm must produce one or more well-defined outputs that are equivalent to the desired output.
 Algorithm must stop or end after a finite number of steps.

NEED FOR ALGORITHM:

 Algorithms are necessary for solving complex problems efficiently and effectively.
 They help to automate processes and make them more reliable, faster, and easier to perform.
 It enables computers to perform tasks that would be difficult or impossible for humans to do manually.
 It is used in various fields such as mathematics, computer science, engineering, finance and many others to
optimize processes, analyze data, make predictions, and provide solutions to problems.
Types of Algorithms:

1. Brute Force Algorithm:

It is the simplest approach to a problem. A brute force algorithm is the first approach that comes to finding when we
see a problem.

2. Recursive Algorithm:

A recursive algorithm is based on recursion. A problem is broken into several sub-parts and called the same
function again and again.

3. Backtracking Algorithm:

The backtracking algorithm builds the solution by searching among all possible solutions. Whenever a
solution fails trace back to the failure point build on the next solution and continues process till find the solution
or all possible solutions are looked after.

4. Searching Algorithm:

Searching algorithms are used for searching elements or groups of elements from a particular data
structure. It can be of different types based on their approach or the data structure in which the element should
be found.

5. Sorting Algorithm:
Sorting is arranging a group of data in a particular manner according to the requirement. The
algorithms which help in performing this function are called sorting algorithms. Generally sorting
algorithms are used to sort groups of data in an increasing or decreasing manner.

6. Hashing Algorithm:

Hashing algorithms work similarly to the searching algorithm. But they contain an index with a key
ID. In hashing, a key is assigned to specific data.

7. Divide and Conquer Algorithm:

It breaks a problem into sub-problems, solves a single sub-problem, and merges the solutions to get the
final solution. It consists of the following three steps:
 Divide
 Solve
 Combine

8. Randomized Algorithm:
In the randomized algorithm, the random number helps in deciding the expected outcome.
9. Greedy Algorithm:

In this type of algorithm, the solution is built part by part. The solution for the next part is built based on the
immediate benefit of the next part. The one solution that gives the most benefit will be chosen as the solution for
the next part.

10. Dynamic Programming Algorithm:

Dynamic Programming Algorithm uses the concept of already found solution to avoid repetitive calculation of
the same part of the problem. It divides the problem into smaller overlapping sub problems and solves them.

Advantages of Algorithms:

 It is easy to understand.
 An algorithm is a step-wise representation of a solution to a given problem.
 In an Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the
programmer to convert it into an actual program.

Disadvantages of Algorithms :

 Writing an algorithm takes a long time so it is time-consuming.


 Understanding complex logic through algorithms can be very difficult.
 Branching and Looping statements are difficult to show in Algorithms (imp).
Example:
Algorithm to multiply 2 numbers and print the result
Step 1: Start
Step 2: Get the knowledge of input. Need for 3 variables; a and b will be the user input and c will hold the result.
Step 3: Declare a, b, c variables.
Step 4: Take input for a and b variable from the user.
Step 5: Know the problem and find the solution using operators, data structures and logic.
We need to multiply a and b variables so we use * operator and assign the result to c. (i.e) c <- a * b
Step 6: Check how to give output, Here we need to print the output. So write print c
Step 7: End
Algorithm to add two numbers entered by the user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Algorithm to find the largest number among three numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop

Algorithm to find Roots of a Quadratic Equation ax2 + bx + c = 0


Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D ← b2-4ac
Step 4: If D ≥ 0
r1 ← (-b+√D)/2a
r2 ← (-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp ← -b/2a
ip ← √(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop

Algorithm to find the factorial of a number


Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
factorial ← 1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
5.1: factorial ← factorial*i
5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop

Algorithm to check whether a number is prime or not


Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables
flag ← 1
i←2
Step 4: Read n from the user.
Step 5: Repeat the steps until i=(n/2)
5.1 If remainder of n÷i equals 0
flag ← 0
Go to step 6
5.2 i ← i+1
Step 6: If flag = 0
Display n is not prime
else
Display n is prime
Step 7: Stop
Algorithm to find the fibonacci series till the term less than 1000
Step 1: Start
Step 2: Declare variables first_term, second_term and temp.
Step 3: Initialize variables first_term ← 0 second_term ← 1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term ≤ 1000
5.1: temp ← second_term
5.2: second_term ← second_term + first_term
5.3: first_term ← temp
5.4: Display second_term
Step 6: Stop

Algorithm to find the maximum of all the elements present in the array.
Step 1: Start the Program
Step 2: Declare a variable max with the value of the first element of the array.
Step 3: Compare max with other elements using loop.
Step 4: If max < array element value, change max to new max.
Step 5: If no element is left, return or print max otherwise goto step 3.
Step 6: End of Solution.

Algorithm to find the average of 3 subjects.


Step 1: Start the Program
Step 2: Declare and Read 3 Subject, let’s say S1, S2, S3
Step 3: Calculate the sum of all the 3 Subject values and store result in Sum variable (Sum = S1+S2+S3)
Step 4: Divide Sum by 3 and assign it to Average variable. (Average = Sum/3)
Step 5: Print the value of Average of 3 Subjects
Step 6: End of Solution

You might also like