0% found this document useful (0 votes)
5 views

Unit I Design And Analysis of Algorithms continued

Design And Analysis of Algorithms for students

Uploaded by

amey.sule0711
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit I Design And Analysis of Algorithms continued

Design And Analysis of Algorithms for students

Uploaded by

amey.sule0711
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit I

Design And Analysis of Algorithms

Presented By - Amrapali Babar


Randomized Algorithms
- An algorithm that uses random numbers to decide what to do next anywhere
in its logic is called a Randomized Algorithm.
Randomized Quick Sort Algorithm
In Randomized Quicksort, we use a random number to pick the next pivot (or we
randomly shuffle the array).
Algorithm Steps:-
1. Choose the first index value as pivot
2. Take two variables to point left and right of the list excluding pivot
3. Left points to the low index
4. Right points to the high
5. While value at left is less than pivot move right
6. While value at right is greater than pivot move left
7. If both step 5 and step 6 does not match swap left and right
8. If left ≥ right, the point where they met is new pivot
Randomized Quick Sort Algorithm Example
0 1 2 3 4 5 6 7 8 9
10 16 8 12 15 6 3 9 5 ∞
l i h
j
Pivot =10 In Simple Language
1 - find sort position of pivot (10)
2 - i = will search elements greater than pivot
j= will search elements smaller than pivot
So that they can exchange
3 - i will stop at infinity and j will stop at pivot
4 - Partitioning procedure - Increment i till you get element greater than 10 and
decrement j till you get element smaller than 10
Algorithm for partitioning
Partition (l,h){
Pivot = A[l];
i=l ; j= h;
do{
i++ ; } while(A[i] < pivot );
}
do{
j - - ; } while (A[j] > pivot);
if(i<j) {
Swap (A[i],A[j] );
}
The General Method
What is the General Method in DAA?

Definition

- The General Method in DAA refers to a systematic approach for creating


interactive and dynamic visualizations of algorithms.
- It involves breaking down an algorithm into smaller, manageable
components, and then visualizing each component using a combination
of graphics, animations, and interactive elements.
- The General Method provides a structured framework for creating DAA
visualizations, enabling users to understand complex algorithms in a
more engaging and intuitive way.
Performance Analysis

- Performance of an algorithm means predicting the resources which are


required to an algorithm to perform its task.
Performance of an algorithm depends on the following elements
1. Whether that algorithm is providing the exact solution for the
problem?
2. Whether it is easy to understand?
3. Whether it is easy to implement?
4. How much space (memory) it requires to solve the problem?
5. How much time it takes to solve the problem? Etc.,
Binary Search Algorithm
- Binary search is a search algorithm used to find the
position of a target value within a sorted array.
- It works by repeatedly dividing the search interval in half
until the target value is found or the interval is empty.

Conditions to apply Binary Search Algorithm

● The data structure must be sorted.


● Access to any element of the data structure should take
constant time.
Step of for Binary Search 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
Example for Binary Search :-
In GIven Array Find Element 23 i.e. Key = 23
● Time Complexity for Binary Search :
○ Best Case: O(1)
○ Average Case: O(log N)
○ Worst Case: O(log N)
Finding Max and Min

Algorithm:
Max: a [i]
Min: a [i]
For i= 2 to n do
If a[i]> max then
max = a[i]
if a[i] < min then
min: a[i]
return (max, min)
Merge Sort
● Merge sort is a sorting algorithm that follows the divide and conquer
approach.
● It works by recursively dividing the input array into smaller sub arrays and
sorting those sub arrays then merging them back together to obtain the sorted
array.
How merge sort works:
● Divide: Divide the list or array recursively into two halves until it can no more
be divided.
● Conquer: Each sub array is sorted individually using the merge sort
algorithm.
● Merge: The sorted sub arrays are merged back together in sorted order. The
process continues until all elements from both sub arrays have been merged.
Recurrence Relation of Merge Sort:

The recurrence relation of merge sort is:


T(n) = { Θ(1) if n=1
2T(2n​)+Θ(n) ​if n>1​
● T(n) Represents the total time time taken by the algorithm to sort an array of
size n.
● 2T(n/2) represents time taken by the algorithm to recursively sort the two
halves of the array. Since each half has n/2 elements, we have two recursive
calls with input size as (n/2).
● O(n) represents the time taken to merge the two sorted halves
● Time Complexity for Merge Sort :
○ Best Case: O(n log n), When the array is already sorted or nearly sorted.
○ Average Case: O(n log n), When the array is randomly ordered.
○ Worst Case: O(n log n), When the array is sorted in reverse order.
Convex Hull Algorithm
● The Convex Hull Algorithm is used to find the convex hull of a set of points
in computational geometry.
● The convex hull is the smallest convex set that encloses all the points,
forming a convex polygon.
● This algorithm is important in various applications such as image processing,
route planning, and object modeling.

Convex Hull
Algorithm Steps:

1) Find the point with minimum x-coordinate lets say, min_x and similarly the
point with maximum x-coordinate, max_x.
2) Make a line joining these two points, say L. This line will divide the whole set
into two parts. Take both the parts one by one and proceed further.
3) For a part, find the point P with maximum distance from the line L. P forms a
triangle with the points min_x, max_x. It is clear that the points residing inside
this triangle can never be the part of convex hull.
4) The above step divides the problem into two sub-problems (solved
recursively). Now the line joining the points P and min_x and the line joining the
points P and max_x are new lines and the points residing outside the triangle is
the set of points. Repeat point no. 3 till there no point left with the line. Add the
end points of this point to the convex hull.
Thank you…..!!!

You might also like