Design Analysis of Algorithms CH 1
Design Analysis of Algorithms CH 1
Design Analysis of Algorithms CH 1
By Arunima Sengupta
What is an Algorithm?
1. The word Algorithm means ” A set of finite rules or instructions
to be followed in calculations or other problem-solving
operations ”
Or
” A procedure for solving a mathematical problem in a finite
number of steps that frequently involves recursive operations”.
2. An algorithm is like a step-by-step recipe or set of instructions
for solving a particular problem. Just as a recipe guides you
through making a dish, an algorithm guides a computer through
solving a task.
Real-life Example: Making a Peanut Butter Sandwich
Let's say you want to create an algorithm for making a peanut butter sandwich.
Here's a simple one:
• Get Ingredients:
– Bread
– Peanut Butter
– Jelly
• Prepare the Bread:
– Take two slices of bread.
• Spread Peanut Butter:
– Use a knife to spread peanut butter on one slice.
• Add Jelly:
– Spread jelly on the other slice.
• Combine Slices:
– Press the peanut butter slice and jelly slice together.
• Cut and Serve:
– Optionally, cut the sandwich into halves or quarters.
Each of these steps is like an instruction in the algorithm. The order is crucial, and
if you follow them correctly, you'll end up with a peanut butter sandwich
Key Characteristics of Algorithms:
• Input: Algorithms take inputs (ingredients in
our example).
• Output: They produce an output or result (a
peanut butter sandwich).
• Clear Steps: Each step is well-defined and
unambiguous.
• Termination: The algorithm stops at some
point (you finish making the sandwich).
• Correctness: If you follow the steps correctly,
you get the desired result.
Characteristics Of Algorithm
More Complex Example: Sorting Cards
Let's say you have a deck of cards in random order, and you want to arrange them in
ascending order. Here's a simple algorithm:
• Start with the First Card:
– Pick the first card.
• Compare with the Next Card:
– Compare it with the next card.
• Swap if Necessary:
– If the first card is greater than the next, swap them.
• Move to the Next Card:
– Move to the next card and repeat steps 2-3.
• Repeat Until Done:
– Keep repeating until you've compared all pairs of cards.
By following these steps, you can organize the cards in ascending order, and this is
essentially a simple sorting algorithm.
In summary, algorithms are like sets of instructions we follow to solve problems, and
we use them in our daily lives, often without realizing it.
Refer
• https://www.geeksforgeeks.org/introduction-
to-algorithms/
What is Performance Analysis-Space
complexity, Time complexity
• Time Complexity:
Time complexity measures how the runtime of
an algorithm grows with the size of the input. In
simpler terms, it tells us how long it takes for an
algorithm to complete as the input gets larger.
• Real-life Example: Making a Sandwich
• Imagine you have a set of instructions for making a
sandwich, and you want to know how the time
complexity relates to the number of steps you
have to follow. If each step takes a fixed amount of
time, and the total time grows linearly with the
number of steps, then the time complexity is said
to be O(n), where "n" is the number of steps.
• For instance, if it takes 1 minute to make a
sandwich with 5 steps, it might take roughly 2
minutes for 10 steps. The time complexity in this
case is linear.
• 2. Space Complexity:
• Space complexity measures how much
additional memory an algorithm needs to
solve a problem, based on the size of the
input. It tells us how the memory usage grows
as the input size increases.
• Real-life Example: Grocery Shopping List
• Think of creating a grocery shopping list as an algorithm with
space complexity. If you write down each item you need on the
list, the space complexity is O(n), where "n" is the number of
items on your list. The more items you need, the more space
(paper) you require.
• For example, if you need 5 items and you write them on a
small piece of paper, you might need a larger piece of paper or
a second sheet if your list grows to 10 items. The space
complexity increases with the number of items on your
shopping list.
• In summary, time complexity is about how the runtime of an
algorithm increases with the size of the input, and space
complexity is about how much additional memory is required
as the input size grows. These concepts help us understand
and compare the efficiency of different algorithms.
• Algorithm ADD SCALAR(A, B)
//Description: Perform arithmetic addition of
two numbers
//Input: Two scalar variables A and B
//Output: variable C, which holds the addition
of A and B
C <- A + B
return C
It is like (<=)
It is like (>=) It is like (==)
rate of growth of an algorithm
1. rate of growth is greater than meaning the rate of growth is
is less than or equal to a
or equal to a specified value. equal to a specified value.
specific value.
3. Big O – Upper Bound Big Omega (Ω) – Lower Bound Big Theta (Θ) – Tight Bound
In this algorithm,
• Divide the search space into two halves by
finding the middle index “mid”.
• Compare the middle element of the search space
with the key.
• If the key is found at middle element, the process
is terminated.
• If the key is not found at middle element, choose
which half will be used as the next search space.
– If the key is smaller than the middle element, then the
left side is used for next search.
– If the key is larger than the middle element, then the
right side is used for next search.
• This process is continued until the key is found or
the total search space is exhausted.
How does Binary Search work?