2 Algorithm
2 Algorithm
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 instance.
NEED OF THE ALGORITHMS:
Algorithms are used to solve problems or automate tasks in a systematic and
efficient manner. They are a set of instructions or rules that guide the computer or
software in performing a particular task or solving a problem.
There are several reasons why we use algorithms:
Efficiency: Algorithms can perform tasks quickly and accurately, making
them an essential tool for tasks that require a lot of calculations or data
processing.
Consistency: Algorithms are repeatable and produce consistent results
every time they are executed. This is important when dealing with large
amounts of data or complex processes.
Scalability: Algorithms can be scaled up to handle large datasets or complex
problems, which makes them useful for applications that require processing
large volumes of data.
Automation: Algorithms can automate repetitive tasks, reducing the need
for human intervention and freeing up time for other tasks.
Standardization: Algorithms can be standardized and shared among
different teams or organizations, making it easier for people to collaborate
and share knowledge.
Overall, algorithms are an essential tool for solving problems in a variety of fields,
including computer science, engineering, data analysis, finance, and many others.
Example:
Consider a box where no one can see what’s happening inside, we say a black box.
We give input to the box and it gives us the output we need but the procedure that
we might need to know behind the conversion of input to desired output is an
ALGORITHM.
An algorithm is independent of the language used. It tells the programmer the logic
used to solve the problem. So, it is a logical step-by-step procedure that acts as a
blueprint to programmers.
Real-life examples that define the use of algorithms:
Consider a clock. We know the clock is ticking but how does the manufacturer
set those nuts and bolts so that it keeps on moving every 60 seconds, the
min hand should move and every 60 mins, the hour hand should move? So to
solve this problem, there must be an algorithm behind it.
Seen someone cooking your favorite food for you? Is the recipe necessary for
it? Yes, it is necessary as a recipe is a sequential procedure that turns a raw
potato into a chilly potato. This is what an algorithm is: following a procedure
to get the desired output. Is the sequence necessary to be followed? Yes, the
sequence is the most important thing that has to be followed to get what we
want.
Types of Algorithms:
Sorting algorithms: Bubble Sort, insertion sort, and many more. These
algorithms are used to sort the data in a particular format.
Searching algorithms: Linear search, binary search, etc. These algorithms
are used in finding a value or record that the user demands.
Graph Algorithms: It is used to find solutions to problems like finding the
shortest path between cities, and real-life problems like traveling salesman
problems.
Sorting algorithms are algorithms that take a collection of elements and
rearrange them in a specified order (e.g. ascending or descending). There are many
different sorting algorithms, each with its own strengths and weaknesses. Some of
the most commonly used sorting algorithms include:
Bubble sort: A simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements and swaps them if they are in the wrong order.
Insertion sort: A simple sorting algorithm that builds up the final sorted array one
item at a time, by comparing each new item to the items that have already been
sorted and inserting it in the correct position.
Selection sort: A simple sorting algorithm that repeatedly selects the minimum
element from the unsorted part of the array and moves it to the end of the sorted
part.
Merge sort: A divide-and-conquer sorting algorithm that works by dividing the
unsorted list into n sub-lists, sorting each sub-list, and then merging them back into
a single sorted list.
Quick sort: A divide-and-conquer sorting algorithm that works by selecting a
“pivot” element from the array and partitioning the other elements into two sub-
arrays, according to whether they are less than or greater than the pivot. The sub-
arrays are then sorted recursively.
Each of these algorithms has different time and space complexities, making some
more suitable for certain use cases than others.
Searching algorithms are algorithms that search for a particular element or value
in a data structure (such as an array or a linked list). Some of the most commonly
used searching algorithms include:
Linear search: A simple searching algorithm that iterates through every element
of a list until it finds a match.
Binary search: A searching algorithm that works by dividing a sorted list in half
repeatedly, until the desired element is found or it can be determined that the
element is not present.
Jump search: A searching algorithm that works by jumping ahead by fixed steps in
the list, until a suitable candidate is found, and then performing a linear search in
the surrounding elements.
Interpolation search: A searching algorithm that works by using information
about the range of values in the list to estimate the position of the desired element
and then verifying that it is indeed present.
Hash table search: A searching algorithm that uses a hash function to map
elements to indices in an array, and then performs constant-time lookups in the
array to find the desired element.
Each of these algorithms has different time and space complexities, making some
more suitable for certain use cases than others. The choice of which algorithm to
use depends on the specific requirements of the problem, such as the size of the
data structure, the distribution of values, and the desired time complexity.
Graph algorithms are a set of algorithms that are used to process, analyze and
understand graph data structures. Graphs are mathematical structures used to
model relationships between objects, where the objects are represented as vertices
(or nodes) and the relationships between them are represented as edges. Graph
algorithms are used in a variety of applications such as network analysis, social
network analysis, recommendation systems, and in many other areas where
understanding the relationships between objects is important. Some of the common
graph algorithms include:
Shortest Path algorithms (e.g. Dijkstra’s, Bellman-Ford, A*)
Minimum Spanning Tree algorithms (e.g. Kruskal, Prim)
Maximum Flow algorithms (e.g. Ford-Fulkerson, Edmonds-Karp)
Network Flow algorithms (e.g. Bipartite Matching)
Connectivity algorithms (e.g. Depth-first Search, Breadth-first Search)
Creating an Algorithm:
Since the algorithm is language-independent, we write the steps to demonstrate the
logic behind the solution to be used for solving a problem. But before writing an
algorithm, keep the following points in mind:
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. After a specific number of steps, algorithms
must ground to a halt.
Algorithms must stop or end after a finite number of steps.
In an algorithm, step-by-step instructions should be supplied, and they should
be independent of any computer code.
Example: algorithm to multiply 2 numbers and print the result:
Step 1: Start
Step 2: Get the knowledge of input. Here we need 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.
That is c <- a * b
Step 6: Check how to give output, Here we need to print the output. So write print
c
Step 7: End
WHAT IS A PSEUDO-CODE?
A pseudocode is a description of an algorithm or a computer program using natural
language. Because the pseudocode is to make reading programs easier, some
codes that are not essential for human understanding are omitted. This language is
commonly used in planning out the structure of a program or a system, like a
blueprint for creating a house or a building.
If a student’s grade is greater than or equal to 75
Print “Passed”
else
Print “Failed”
WHAT IS A FLOWCHART?
Like a pseudocode, the flowchart is also a description of an algorithm or a computer
program. It also serves as the program’s blueprint during the Program Development
Process. The difference is that a flowchart is a graphical representation of it.
Flowcharts help in the effective analysis of the problem as well as the application’s
or program’s maintenance, thus providing ease in identifying potential
improvements to the system or program.
It consists of 7 standard symbols namely:
1. Terminator
2. Process
3. Decision
4. Input/output Operation
5. Arrow
6. On-page Connector
7. Off-page Connector