Focp Ut 1 Notes
Focp Ut 1 Notes
Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be clear in all
aspects and must lead to only one meaning.
Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It may or may not
take input.
Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-
defined as well. It should produce at least 1 output.
Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available
resources. It must not contain some future technology or anything.
Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain
instructions that can be implemented in any language, and yet the output will be the same, as expected.
Input: An algorithm has zero or more inputs. Each that contains a fundamental operator must accept zero or
more inputs.
Output: An algorithm produces at least one output. Every instruction that contains a fundamental operator
must accept zero or more inputs.
Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy to interpret. By referring
to any of the instructions in an algorithm one can clearly understand what is to be done. Every fundamental
operator in instruction must be defined without any ambiguity.
Finiteness: An algorithm must terminate after a finite number of steps in all test cases. Every instruction which
contains a fundamental operator must be terminated within a finite amount of time. Infinite loops or recursive
functions without base conditions do not possess finiteness.
Effectiveness: An algorithm must be developed by using very basic, simple, and feasible operations so that one
can trace it out by using just paper and pencil.
Properties of Algorithm:
It should terminate after a finite time.
It should produce at least one output.
It should take zero or more input.
It should be deterministic means giving the same output for the same input case.
Every step in the algorithm must be effective i.e. every step should do some work.
Types of Algorithms:
There are several types of algorithms available. Some important algorithms are:
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. In this case, 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. Using this algorithm, we
keep on building the solution following criteria. Whenever a solution fails we trace back to the failure point build on
the next solution and continue this process till we find the solution or all possible solutions are looked after.
4. Searching Algorithm:
Searching algorithms are the ones that are used for searching elements or groups of elements from a particular data
structure. They 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:
This algorithm 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. 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.
9. Dynamic Programming Algorithm:
This algorithm uses the concept of using the already found solution to avoid repetitive calculation of the same part
of the problem. It divides the problem into smaller overlapping subproblems and solves them.
10. Randomized Algorithm:
In the randomized algorithm, we use a random number so it gives immediate benefit. The random number helps in
deciding the expected outcome.
To learn more about the types of algorithms refer to the article about “Types of Algorithms“.
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).
Flow chart:
Flowcharts are the visual representations of an algorithm or a process. Flowcharts use symbols/shapes like
arrows, rectangles, and diamonds to properly explain the sequence of steps involved in the algorithm or process.
1. Terminal/Terminator
The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A pause/halt is generally used in a
program logic under some error conditions. Terminal is the first and last symbols in the flowchart.
2. Input/Output
A parallelogram denotes any function of input/output type. Program instructions that take input from input
devices and display output on output devices are indicated with parallelogram in a flowchart.
3. Action/Process
A box represents arithmetic instructions, specific action or operation that occurs as a part of the process. All
arithmetic processes such as adding, subtracting, multiplication and division are indicated by action/process
symbol.
4. Decision
Diamond symbol represents a decision point. Decision based operations such as yes/no question or true/false are
indicated by diamond in flowchart.
5. On-Page Connector/Reference
Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use connectors to
avoid any confusions. connectors are used to indicate a jump from one part of the flowchart to another without
drawing long or complicated lines. On-Page Connector is represented by a small circle
6. Off-Page Connector/Reference
Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use connectors to
avoid any confusions. connectors are used to indicate a jump from one part of the flowchart to another without
drawing long or complicated lines. Off-Page Connector is represented by a pentagon.
7. Flow lines
Flow lines indicate the exact sequence in which instructions are executed. Arrows represent the direction of flow
of control and relationship among different symbols of flowchart.
Example of a Flowchart
Draw a flowchart to input two numbers from the user and display the largest of two numbers.
Write an algorithm (in pseudocode) to calculate the factorial of a number. Discuss its time complexity and
space complexity. How can the algorithm be optimized to reduce space usage?
if n == 0 or n == 1 then
return 1
else
result ← 1
for i ← 2 to n do
result ← result * i
end for
return result
end if
End Algorithm
Time Complexity:
The time complexity of this algorithm is O(n) because the loop iterates from 2 to n, performing one multiplication
per iteration.
Space Complexity:
The space complexity is O(1) because the algorithm uses a constant amount of space for variables (result and i),
regardless of the input size.
2. Iterative Implementation: Switching to an iterative approach (as provided above) reduces space
complexity to O(1), as it avoids recursion and uses only a single variable to store intermediate results.
sum ← 0
while n > 0 do
digit ← n mod 10 // Get the last digit
sum ← sum + digit // Add the digit to the sum
n ← n div 10 // Remove the last digit from n
end while
return sum
End Algorithm
Conclusion:
Time Complexity: O(d), where dd is the number of digits.
Space Complexity: O(1).
The time complexity increases linearly with the number of digits dd, but the space complexity remains constant,
making the algorithm highly efficient in terms of memory usage.
Case study:
1. Factorial of a Number
Input: Take a positive integer 'n' as input.
Initialize: Set a variable 'factorial' to 1.
Loop: Iterate through numbers from 1 to 'n'.
Multiply: In each iteration, multiply the current 'factorial' value by the current number.
Output: After the loop, the final value of 'factorial' is the factorial of 'n'.
Algorithm Steps:
1. Start
2. Read input: Get the positive integer 'n' from the user.
3. Initialize: Set 'factorial' variable to 1.
4. Loop:
For 'i' from 1 to 'n':
'factorial' = 'factorial' * 'i'
5. Print result: Display the calculated 'factorial' value.
6. End
3. Sorting in a Array
Problem Statement – How to arrange array in ascending order
Sorting is the process of arranging elements in a list or array in a specific order, typically in ascending or descending
order. Sorting is a fundamental problem in computer science and has many applications in areas such as searching,
data compression, and data analysis.
There are many sorting algorithms that can be used to sort an array. Some of the most popular algorithms include:
1. Bubble Sort: This is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order.
2. Selection Sort: This algorithm sorts an array by repeatedly finding the minimum element from the
unsorted part of the array and putting it at the beginning.
3. Insertion Sort: This algorithm builds the final sorted array one item at a time, by inserting each item in the
correct position in the array.
4. Merge Sort: This algorithm divides the array into two halves, sorts each half separately, and then merges
the two halves together to form a sorted array.
5. Quick Sort: This algorithm picks an element as a pivot and partitions the array around the pivot, such that
elements smaller than the pivot are on one side and elements larger than the pivot are on the other side. It
then recursively sorts the two sub-arrays.
Algorithm :
Take the size of the array from the user.
Declare an array of given input size.
Take the input of all elements of the array.
Now run a for loop from 0 to size-1.
And for every element check it from all the next elements to it. If the element is greater than swap that
number.
In this way the array will get sorted in ascending order.
Searching an array
What is an Array?
A data structure called an array holds a fixed-length series of identical-type items. It is frequently used to store and
manipulate data collections because indexing enables efficient access.
Ex: intnumbers[] = {10, 20, 30, 40, 50};
Searching an Element in an Array
A typical operation in computer programming is looking for a particular element in an array. The efficiency of your
code may be greatly improved by using efficient searching algorithms whether you are searching for the existence of
a certain value locating the index of an element, or verifying if an element exists. The many methods for searching
for elements in an array using the C programming language will be discussed in this article.
There are mainly two ways to Search an Element in an Array:
1. Linear Search
A straightforward search strategy used to locate a given element in an array or list is called linear search, sometimes
referred to as sequential search. It operates by comparing each array member to the target value to find
a match or traverse the full array iteratively.
The fundamental steps in linear search are as follows:
1. Start with the array's topmost elements.
2. The target value should be compared to the current element.
3. The search is successful if the current element matches the requested value, and then the algorithm can
return the element's index or any other desired output.
4. Go to the following element in the array if the current element does not match the desired value.
5. Until a match is made or the end of the array is reached, repeat steps 2-4.