DAA Unit1
DAA Unit1
DAA Unit1
(2022-2023)
Unit -I Notes
INDEX
1
S. No Topic From page To Page
1 Course Outcomes 3 3
2 Algorithm 4 4
3 Analysis of Algorithm 4 7
4 Time Complexity 8 9
Analysis and
Calculation
5 Master’s Theorem
Searching Algorithms
6 9 9
10 15
7 Sorting Algorithms 16 26
2
ALGORITHM
A finite set of instruction that specifies a sequence of operation is to be carried
out in order to solve a specific problem or class of problems is called an
Algorithm.
Example:
Q. Write an algorithm for travelling to New Delhi.
Algorithm:
1. Go to an appropriate portal for booking the ticket for a train i.e.
an mobile application or a website.
2. Input the necessary details like boarding and destination station, date of
boarding and the desired class.
3. Select a train from the given options.
4. Enter the number of passengers boarding the train and their respective
details.
5. Select the mode of payment and pay the required amount to the payment
portal.
6. Collect the now confirmed tickets.
7. Reach the railway station on required time and board the train from the
appropriate station.
8. Finally leave the train at the destination.
Analysis of Algorithms
Analysis of algorithm is the process of analyzing the problem-solving capability
of the algorithm in terms of the time and size required (the size of memory for
storage while implementation). However, the main concern of analysis of
algorithms is the required time or performance.
3
execution of the algorithm. The memory space is generally considered as the
primary memory.
Asymptotic analysis
4
Hence, the complexity of f(n) can be represented as O (g (n)).
3. Theta (θ): The function f (n) = θ (g (n)) [read as "f is the theta of g of n"]
if and only if there exists positive constant k 1, k2 and k0 such that k1 * g (n) ≤
f(n)≤ k2 g(n)for all n, n≥ n0
5
Hence, the complexity of f (n) can be represented as θ (g(n)).
6
Time Complexity Analysis and Calculation
Q1. T(n)=T(n-1)+2
T(n-1)=T(n-2)+2
T(n-2)=T(n-3)+2
|
|
|
|
T(n)=T(n-4)+2+2+2+2….. =T(n-k)
+2+2+2…k times n-k=0
=>n=k
=T(0)+2n
=1+2n
=>O(n)
Q2. T(n)=2T(n-1)+1
T(n-1)=2T(n-2)+1
T(n-2)=2T(n-3)+1
|
|
|
|
T(n)=2[2T(n-3)+1]+1
= 4T(n-3)+3;
=2[4T(n-3)+2+1]+1
= 8T(n-3) +4+2+1
T(n)=2^K[T(n-k)]+2^K-1
=2^n + 2^n -1
=2^(n+1)-1
=>O(2^n)
7
Master’s Theorem
T(n) = aT(n/b) + f(n)
ϵ > 0 is a constant.
Example 1:
T(n) = 3T(n/2) + n^2
Here, a =
3 n/b =
n/2
f(n) = n2
8
Searching Algothims
Linear Search
Algorithm:-
Linear Search ( Array Arr, Value a ) // Arr is the name of the array, and a is the
searched element.
Step 1: Set i to 0 // i is the index of an array which starts from 0
Step 2: if i > n then go to step 7 // n is the number of elements in array
Step 3: if Arr[i] = a then go to step 6
Step 4: Set i to i + 1
Step 5: Go to step 2
Step 6: Print element a found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
C++ Code:
// C++ code to linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1
9
return i;
return -1;
}
12
if (arr[m] < x)
l = m + 1;
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]); int result =
binarySearch(arr, 0, n - 1, x);
(result == -1)
? cout << "Element is not present in array" : cout
<< "Element is present at index " << result;
return 0;
}
13
o Best Case Complexity - In Binary search, best case occurs when the
element we are finding is at the mid position of the array. The best-case
time complexity of binary search is O(1).
o Average Case Complexity - The average case time complexity of binary
search is O(logn).
o Worst Case Complexity - The worst-case in binary search could be when
the target element is not present in the given array, and we have to
traverse the entire array. The worst-case time complexity of binary search
is O(logn).
14
Sorting Algorithms
Insertion-Sort
Algorithm
INSERTION-SORT. (A)
1. for I = 2 to A.length
2. key = A[i]
3. j =I–1
4. while j = 0 and A[j] > key
5. A[ j + 1] = A[ j ]
6. j=j–1
7. A[ j + 1 ] = key
Program
Output :
3 7 11 12 13
Merge Sort
Algorithm
15
step 1: start
step 2: declare array and left, right, mid variable step 3:
perform merge function.
if left > right return mid=
(left+right)/2 mergesort(array, left,
mid) mergesort(array, mid+1, right)
merge(array, left, mid, right) step 4:
Stop
// Copy data to temp arrays leftArray[] and rightArray[] for (auto i = 0; i <
subArrayOne; i++)
leftArray[i] = array[left + i]; for (auto j = 0; j <
subArrayTwo; j++)
rightArray[j] = array[mid + 1 + j];
auto indexOfSubArrayOne
16
= 0, // Initial index of first sub-array
indexOfSubArrayTwo
= 0; // Initial index of second sub-array int
indexOfMergedArray
= left; // Initial index of merged array
17
// begin is for left index and end is
// right index of the sub-array // of
arr to be sorted */
void mergeSort(int array[], int const begin, int const end)
{
if (begin >= end)
return; // Returns recursively
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
auto arr_size = sizeof(arr) / sizeof(arr[0]);
Bubble Sort
Algorithm
Begin BubbleSort(list)
18
for all elements of list
end if
end
for
return list
end BubbleSort
Time Complexity: O( n2 )
Program
def bubblesort(elements):
swapped = False
swapped = True
19
elements[i], elements[i + 1] = elements[i + 1], elements[i]
if not swapped:
return
print(elements)
bubblesort(elements)
print(elements)
Output:
Algorithm
20
Step 2 − Search the minimum element in the list
Program
min_index = ind
min_index = j
size = len(arr)
selectionSort(arr, size)
Output
22
Quick Sort
Algorithm
Program
24
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver Code
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5}; int n =
sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1); cout << "Sorted
array: \n";
printArray(arr, n);
return 0;
}
25