Unit-I DAA
Unit-I DAA
Analysis of algorithm
The analysis is a process of estimating the efficiency of an
algorithm. There are two fundamental parameters based on which
we can analysis the algorithm:
Complexity of Algorithm
The term algorithm complexity measures how many steps are
required by the algorithm to solve the given problem. It evaluates
the order of count of operations executed by an algorithm as a
function of input data size.
1
The complexity can be found in any form such as constant,
logarithmic, linear, n*log(n), quadratic, cubic, exponential, etc. It is
nothing but the order of constant, logarithmic, linear and so on, the
number of steps encountered for the completion of a particular
algorithm. To make it even more precise, we often call the
complexity of an algorithm as "running time".
Asymptotic Notations:
Asymptotic Notation is used to describe the running time of an algorithm -
how much time an algorithm takes with a given input, n. There are three
different notations: big O, big Theta (Θ), and big Omega (Ω).
3
Big-O notation represents the upper bound of the running time of an
algorithm. Therefore, it gives the worst-case complexity of an
algorithm.
If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there
exist a positive constant C and n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥
n0
It returns the highest possible output value (big-O)for a given
input.
The execution time serves as an upper bound on the algorithm’s
time complexity.
Properties of Recursion:
6
If the target element is less than the element at the middle index,
set the high index to the middle index – 1.
If the target element is greater than the element at the middle index,
set the low index to the middle index + 1.
Repeat steps 3-6 until the element is found or it is clear that the
element is not present in the array.
Bubble Sort
Bubble Sort, also known as Exchange Sort, is a simple sorting
algorithm. It works by repeatedly stepping throughout the list to be
sorted, comparing two items at a time and swapping them if they
are in the wrong order. The pass through the list is duplicated until
no swaps are desired, which means the list is sorted.
For each iteration, the bubble sort will compare up to the last
unsorted element.
Once all the elements get sorted in the ascending order, the
algorithm will get terminated.
7
Consider the following example of an unsorted array that we will
sort with the help of the Bubble Sort algorithm.
Initially,
Pass 1:
o Compare a0 and a1
o Compare a1 and a2
8
o Compare a2 and a3
o Compare a3 and a4
Pass 2:
o Compare a0 and a1
9
As a0 < a1 so the array will remain as it is.
o Compare a1 and a2
o Compare a2 and a3
Pass 3:
10
o Compare a0 and a1
o Compare a1 and a2
Pass 4:
o Compare a0 and a1
11
Here a0 > a1, so we will swap both of them.
12
necessitates some extra memory space for temp variable for
swapping.
Time Complexities:
o Best Case Complexity: The bubble sort algorithm has a best-
case time complexity of O(n) for the already sorted array.
o Average Case Complexity: The average-case time
complexity for the bubble sort algorithm is O(n2), which
happens when 2 or more elements are in jumbled, i.e., neither
in the ascending order nor in the descending order.
o Worst Case Complexity: The worst-case time complexity is
also O(n2), which occurs when we sort the descending order of
an array into the ascending order.
Selection Sort
The selection sort enhances the bubble sort by making only a single
swap for each pass through the rundown. In order to do this, a
selection sort searches for the biggest value as it makes a pass and,
after finishing the pass, places it in the best possible area. Similarly,
as with a bubble sort, after the first pass, the biggest item is in the
right place. After the second pass, the following biggest is set up.
This procedure proceeds and requires n-1 goes to sort n item since
the last item must be set up after the (n-1) th pass.
A [] = (7, 4, 3, 6, 5).
A [] =
14
1st Iteration:
Set minimum = 7
o Compare a0 and a1
Binary Search
1. In Binary Search technique, we search an element in a sorted
array by recursively dividing the interval in half.
Analysis:
1. Input: an array A of size n, already sorted in the ascending or
descending order.
15
2. Output: analyze to search an element item in the sorted array
of size n.
3. Logic: Let T (n) = number of comparisons of an item with n
elements in a sorted array.
o Find mid =
o Compare the search item with the mid item.
Two popular search methods are Linear Search and Binary Search.
So, here we will discuss the popular searching technique, i.e., Linear
Search Algorithm.
int i;
16
if (arr[i] == x)
return i;
return -1;
// Driver's code
int main(void)
int x = 10;
// Function call
(result == -1)
return 0;
17
}
Output
Element is present at index 3
1. n length[p]-1
2. for i ← 1 to n
3. do m [i, i] ← 0
4. for l ← 2 to n // l is the chain length
5. do for i ← 1 to n-l + 1
6. do j ← i+ l -1
7. m[i,j] ← ∞
8. for k ← i to j-1
9. do q ← m [i, k] + m [k + 1, j] + pi-1 pk pj
10. If q < m [i,j]
11. then m [i,j] ← q
12. s [i,j] ← k
13. return m and s.
Step 1: Constructing an Optimal Solution:
PRINT-OPTIMAL-PARENS (s, i, j)
1. if i=j
2. then print "A"
3. else print "("
4. PRINT-OPTIMAL-PARENS (s, i, s [i, j])
5. PRINT-OPTIMAL-PARENS (s, s [i, j] + 1, j)
6. print ")"
18
The main idea of asymptotic analysis is to have a measure of the
efficiency of algorithms that don’t depend on machine-specific
constants and don’t require algorithms to be implemented and time
taken by programs to be compared. Asymptotic notations are
mathematical tools to represent the time complexity of algorithms for
asymptotic analysis.
19
Insertion sort
Insertion sort is a very simple method to sort numbers in an
ascending or descending order. This method follows the incremental
method. It can be compared with the technique how cards are
sorted at the time of playing a game.
20
Pseudocode
Algorithm: Insertion-Sort(A)
for j = 2 to A.length
key = A[j]
i=j–1
while i > 0 and A[i] > key
A[i + 1] = A[i]
i = i -1
A[i + 1] = key
Analysis
If the given numbers are sorted, this algorithm runs in O(n) time. If
the given numbers are in reverse order, the algorithm runs
in O(n2) time.
Example
It finds that both 14 and 33 are already in ascending order. For now,
14 is in sorted sub-list.
21
Insertion sort moves ahead and compares 33 with 27.
And finds that 33 is not in the correct position. It swaps 33 with 27.
It also checks with all the elements of sorted sub-list. Here we see
that the sorted sub-list has only one element 14, and 27 is greater
than 14. Hence, the sorted sub-list remains sorted after swapping.
22
However, swapping makes 27 and 10 unsorted.
23
This process goes on until all the unsorted values are covered in a
sorted sub-list. Now we shall see some programming aspects of
insertion sort.
Implementation
Since insertion sort is an in-place sorting algorithm, the algorithm is
implemented in a way where the key element – which is iteratively
chosen as every element in the array – is compared with it
consequent elements to check its position. If the key element is less
than its successive element, the swapping is not done. Otherwise,
the two elements compared will be swapped and the next element
is chosen as the key element.
#include <stdio.h>
void insertionSort(int array[], int size){
int key, j;
for(int i = 1; i<size; i++) {
key = array[i];//take value
j = i;
while(j > 0 && array[j-1]>key) {
array[j] = array[j-1];
j--;
}
array[j] = key; //insert in right place
}
}
int main(){
int n;
n = 5;
int arr[5] = {67, 44, 82, 17, 20}; // initialize the array
printf("Array before Sorting: ");
for(int i = 0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");
24
insertionSort(arr, n);
printf("Array after Sorting: ");
for(int i = 0; i<n; i++)
printf("%d ", arr[i]);
printf("\n");
}
Output
Array before Sorting: 67 44 82 17 20
Array after Sorting: 17 20 44 67 82
25