Assignment 1 AoA
Assignment 1 AoA
Algorithm
1. Binary search -> Binary search is a classic algorithm, which, given a
target value and an ordered list or array, determines whether the
value appears in the list and, if so, its position. It performs this by
repeatedly dividing the search interval in half. If the value of the
search key is less than the item in the middle of the interval, the
interval is narrowed to the lower half. Otherwise, the interval is
narrowed to the upper half. This continues until the desired value
has been found, or else the interval becomes empty.
Binary Search Algorithm (Recursive Approach):
1. Base Case:
o If left exceeds right, return -1 (indicating the target is not
found).
2. Calculate Middle Index:
o Calculate mid = left + (right - left) / 2.
3. Check Middle Element:
o If the target value equals arr[mid], return mid.
o If the target value is less than arr[mid], recursively search the
left subarray: binarySearch(arr, left, mid - 1, target).
o If the target value is greater than arr[mid], recursively search
the right subarray: binarySearch(arr, mid + 1, right, target).
4. Return:
o The result of the recursive call.
1. Choose a Pivot:
• Select a pivot element from the array (commonly the last element,
first element, or a random element).
2. Partition the Array:
• Rearrange the array such that:
o Elements less than the pivot are on the left.
o Elements greater than the pivot are on the right.
• The pivot element will be in its final sorted position after the
partitioning process.
3. Recursively Apply Quick Sort:
• Apply the quick sort algorithm recursively to the sub-arrays
formed by the partition step:
o The sub-array of elements less than the pivot.
o The sub-array of elements greater than the pivot.
4. Base Case:
• If the sub-array has zero or one element, it is already sorted, and
no further action is needed.
1 Divide:
• Divide the unsorted array into two approximately equal halves.
2 Conquer:
• Recursively apply merge sort to the two halves until each sub-
array contains only one element.
3 Merge:
• Merge the two halves back together in a sorted manner.
4 Base Case:
• If the array has zero or one element, it is already sorted, and no
further action is needed.
2 . Notations.
Notations in algorithm analysis are mathematical tools used to
describe and analyze the performance of algorithms, particularly in
terms of time and space complexity. These notations help in
understanding how an algorithm's resource usage grows with the
size of the input.
Common Notations:
1. Big O Notation (O)
2. Big Omega Notation (Ω)
3. Big Theta Notation (Θ)
4. Little o Notation (o)
5. Little omega Notation (ω)
3. Recurrence Relations->
A recurrence relation is an equation that recursively defines a
sequence of values. Each term in the sequence is defined as a function
of one or more previous terms. Recurrence relations are commonly
used in computer science and mathematics to describe the runtime of
recursive algorithms or to model dynamic systems.
4. Substitution (Iteration):
Step-by-Step Solution
1. Understand the Recurrence:
o T(n)) represents the time it takes to perform binary search on
an array of size n.
o The algorithm divides the array into two halves, so the
problem size reduces from n to n2
o The O(1) term represents the constant time operations (like
checking the middle element).
2. Unroll the Recurrence (also known as substitution method):
• Start by expanding the recurrence relation by substituting T(n) with
its definition: T(n)=T(n/2)+O(1)
• Now, substitute T(n2) with its recurrence: T(n)=[T(n/4)+O(1)]+O(1)
• Substitute again: T(n)=[[T(n/8)+O(1)]+O(1)]+O(1)
• After k steps, the recurrence relation becomes:
T(n)=T(n/2^k)+k⋅O(1)
3.Determine the Base Case: The base case occurs when the size of the
problem is reduced to 1, i.e., n/2^k=1
Solve for k: n/2^k=1⇒n=2^k⇒k=log2n
4. Substitute the Base Case:
• At the base case k=log2n, we substitute back into the recurrence:
T(n)=T(1)+log2n⋅O(1)
• T(1) is a constant, say O(1), because checking an array of size 1 takes
constant time.
Conclusion
The time complexity of binary search, as derived from the recurrence
relation T(n)=T(n/2)+O(1), is O(logn). This reflects the logarithmic nature
of binary search, where the problem size is halved with each recursive
step.
T(n)=T(n-1)+n ---------------------1
T(n-1) = T(n-2)+n-1 -------------2
T(n-2) = T(n-3) + n-2 -------------3
Substitute T(n-1) in place of n in equation 1
T(n) = T(1)+2+3+4……………..(n-1)+ n
T(n) = n(n+1)/2 = n^2/2 +n^2 /2 = O(n^2)
Time Complexity
• Best Case: O(nlogn)
• Average Case: O(nlogn)
• Worst Case: O(nlogn)
return final_solution