0% found this document useful (0 votes)
72 views30 pages

Unit 1 Rahul

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views30 pages

Unit 1 Rahul

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Course Name: Problem Solving

Unit-1
P R E PA R E D B Y:
RAHUL KUMAR
A S S I S TA N T P R O F E S S O R
SCHOOL OF COMPUTER SCIENCE
UPES DEHRADUN
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.
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 Algorithm
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.
Importance of Algo
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.
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.
Algorithm Points:
•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
Example 2: Write an algorithm to find the average of 3 subjects.

Follow the algorithm approach as below:

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


Flow Chart
A flowchart is a type of diagram that represents a
workflow or process. A flowchart can also be defined
as a diagrammatic representation of an algorithm, a
step-by-step approach to solving a task.
Symbol Name Symbol Representation

Terminal/Terminator

Process

Decision

Document

Data/or input data

Stored Data

Flow Arrow
Symbol Name Symbol Representation
Pre-defined Process

On Page Connector

Off Page Connector


On Page Connector
Uses of Flowcharts in Computer Programming/Algorithms
The following are the uses of a flowchart:
•It is a pictorial representation of an algorithm that increases the readability of the
program.
•Complex programs can be drawn in a simple way using a flowchart.
•It helps team members get an insight into the process and use this knowledge to
collect data, detect problems, develop software, etc.
•A flowchart is a basic step for designing a new process or adding extra features.
•Communication with other people becomes easy by drawing flowcharts and
sharing them.
Flowcharts are mainly used in the below scenarios:
•It is most importantly used when programmers make projects. As a flowchart is a
basic step to make the design of projects pictorially, it is preferred by many.
•When the flowcharts of a process are drawn, the programmer understands the
non-useful parts of the process. So flowcharts are used to separate sound logic
from the unwanted parts.
•Since the rules and procedures of drawing a flowchart are universal, a flowchart
serves as a communication channel to the people who are working on the same
project for better understanding.
•Optimizing a process becomes easier with flowcharts. The efficiency of the code
is improved with the flowchart drawing.
Draw a flowchart to find the greatest number among the 2
numbers.
Draw a flowchart to check whether the input number is
odd or even
Algorithm Complexity:

Complexity in algorithms refers to the amount of resources (such as time or memory) required to

solve a problem or perform a task. The most common measure of complexity is time complexity,

which refers to the amount of time an algorithm takes to produce a result as a function of the size

of the input. Memory complexity refers to the amount of memory used by an algorithm.

Algorithm designers strive to develop algorithms with the lowest possible time and memory

complexities, since this makes them more efficient and scalable.


The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the

amount of data the algorithm must process.

Usually there are natural units for the domain and range of this function.

An algorithm is analyzed using Time Complexity and Space Complexity. Writing an efficient algorithm

help to consume the minimum amount of time for processing the logic. For algorithm A, it is judged on

the basis of two parameters for an input of size n


Time Complexity: Time taken by the algorithm to solve the problem.
It is measured by calculating the iteration of loops, number of
comparisons etc.
Time complexity is a function describing the amount of time an
algorithm takes in terms of the amount of input to the algorithm.
“Time” can mean the number of memory accesses performed, the
number of comparisons between integers, the number of times some
inner loop is executed, or some other natural unit related to the
amount of real time the algorithm will take.
Space Complexity: Space taken by the algorithm to solve the problem. It includes space used by
necessary input variables and any extra space (excluding the space taken by inputs) that is used by
the algorithm. For example, if we use a hash table (a kind of data structure), we need an array to
store values so

•this is an extra space occupied, hence will count towards the space complexity of the algorithm.
This extra space is known as Auxiliary Space.

•Space complexity is a function describing the amount of memory(space)an algorithm takes in terms
of the amount of input to the algorithm.

•Space complexity is sometimes ignored because the space used is minimal and/ or obvious, but
sometimes it becomes an issue as time.
Pseudo code
This is a term which is often used in programming and algorithm based fields. It is a
methodology that allows the programmer to represent the implementation of an
algorithm. Simply, we can say that it’s the cooked up representation of an algorithm.
Often at times, algorithms are represented with the help of pseudo codes as they can be
interpreted by programmers no matter what their programming background or
knowledge is. Pseudo code, as the name suggests, is a false code or a representation of
code which can be understood by even a layman with some school level programming
knowledge.
Pseudo code: It’s simply an implementation of an algorithm in the form of

annotations and informative text written in plain English. It has no syntax like

any of the programming language and thus can’t be compiled or interpreted by

the computer.
Advantages of Pseudocode

•Improves the readability of any approach. It’s one of the best approaches to start

implementation of an algorithm.

•Acts as a bridge between the program and the algorithm or flowchart. Also works as a

rough documentation, so the program of one developer can be understood easily when

a pseudo code is written out. In industries, the approach of documentation is essential.

And that’s where a pseudo-code proves vital.

•The main goal of a pseudo code is to explain what exactly each line of a program

should do, hence making the code construction phase easier for the programmer.
Example:

if "1"
print response
"I am case 1"

if "2"
print response
"I am case 2"
S. No Algorithm Flowchart

An algorithm is a step-by-step procedure A flowchart is a diagram created with


1.
to solve a problem. different shapes to show the flow of data.

2. The algorithm is complex to understand. A flowchart is easy to understand.

3. In the algorithm, plain text is used. In the flowchart, symbols/shapes are used.

4. The algorithm is easy to debug. A flowchart is hard to debug.

5. The algorithm is difficult to construct. A flowchart is simple to construct.

The flowchart follows rules to be


6. The algorithm does not follow any rules.
constructed.

The algorithm is the pseudo-code for the A flowchart is just a graphical representation
7.
program. of that logic.
How Does Linear Search Algorithm Work?

In Linear Search Algorithm,

•Every element is considered as a potential match for the key and checked for the

same.

•If any element is found equal to the key, the search is successful and the index of

that element is returned.

•If no element is found equal to the key, the search yields “No match found”.

For example: Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key =

30
Advantages of Linear Search:
•Linear search can be used irrespective of whether the array is sorted or not. It can be used on
arrays of any data type.
•Does not require any additional memory.
•It is a well-suited algorithm for small datasets.
Drawbacks of Linear Search:
•Linear search has a time complexity of O(N), which in turn makes it slow for large datasets.
•Not suitable for large arrays.
When to use Linear Search?
•When we are dealing with a small dataset.
•When you are searching for a dataset stored in contiguous memory.
Binary Search
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm

works on the principle of divide and conquer. For this algorithm to work properly, the data collection

should be in the sorted form.

Binary search looks for a particular item by comparing the middle most item of the collection. If a

match occurs, then the index of item is returned. If the middle item is greater than the item, then the

item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in

the sub-array to the right of the middle item. This process continues on the sub-array as well until the

size of the subarray reduces to zero.


Procedure binary_search A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists. set
midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x set lowerBound = midPoint + 1
if A[midPoint] > x set upperBound = midPoint - 1
if A[midPoint] = x EXIT:
x found at location midpoint
end while

You might also like