Ch-1: Algorithms Analysis
Ch-1: Algorithms Analysis
Ch-1: Algorithms Analysis
In algorithm analysis, the rate of growth refers to how the running time of an
algorithm increases as the input size grows. It's a measure of the algorithm's
efficiency and scalability.
2 3 𝑛 𝑛 𝑛
1 < 𝑙𝑜𝑔𝑛 < 𝑛 < 𝑛𝑙𝑜𝑔𝑛 < 𝑛 < 𝑛 < ... < 2 < 3 < 𝑛
Key points:
● Big O notation is used to describe the upper bound of a function's growth. - Worst
● Big Omega notation is used to describe the lower bound of a function's growth. - Best
● Big Theta notation is used to describe avg bounds of a function's growth. - Avg
2) Nested loops: Analyze from the inside out. Total running time is the product of
2 2
total = 𝑛 + 𝑛 = 𝑂(𝑛 )
4) If-then-else statements:
Worst-case running time: the
test, plus either the then
part or the else part (whichever is the larger).
//test: constant
if (length() == 0) {
return false; // then part: constant
}
else { // else part: (c + c) * n
for (int n = 0; n < length(); n++) {
// another if: c+ c
if (!list[n].equals(otherList.list[n]))
// constant
return false;
}
}
Total time = c₀ + c₁ + (c₂ + c₃) * n = O(n)
Examples
2
total = 𝑛/2 * 3𝑛/2 * 𝑙𝑜𝑔(𝑛) = 𝑂(𝑛 𝑙𝑜𝑔(𝑛))
2
total = 𝑛/2 * 𝑙𝑜𝑔(𝑛) * 𝑙𝑜𝑔(𝑛) = 𝑂(𝑛 𝑙𝑜𝑔 (𝑛))
total = 𝑛 * 𝑐 = 𝑂(𝑛)
linear time
Cubic:
Quadratic
● Find the maximum crossing sum: This is the sum of a subsequence that starts in the left half and
ends in the right half. Iterate from the middle towards the left and right ends, keeping track of the
maximum sum seen so far.
● Return the maximum of the left sum, right sum, and crossing sum.
Time complexity:
The algorithm has a time complexity of O(n log n). This is because the array is divided in half at each
recursion level, and the combining step takes linear time.
Linear time
Tricky parts of this algorithm are:
ans:
The worst-case running time for the linearMCCS algorithm is O(n), while the
worst-case running time for the quadraticMCCS algorithm is O(n^2).
Steps:
Time complexity:
The algorithm iterates through the array once, performing constant-time operations
at each step. Therefore, the worst-case running time is O(n).
The brute force algorithm for finding the maximum contiguous subarray sum checks
all possible subarrays and calculates their sums. The worst-case occurs when the
maximum subarray is the entire array.
Steps:
Time complexity:
The outer loop iterates n times, and the inner loop iterates at most n times for each
outer loop iteration. Therefore, the total number of iterations is O(n^2).
● Big-O: The worst-case running time for both algorithms can be expressed in
big-O notation as O(n) for linearMCCS and O(n^2) for quadraticMCCS.
● Big-Omega: The best-case running time for both algorithms is also O(n) and
O(n^2), respectively. This is because the algorithms must at least examine
every element in the array to find the maximum contiguous subarray sum.
Example:
The worst-case time complexity of an algorithm that divides arrays in two parts in each
iteration is O(log n).
This is because each iteration effectively halves the size of the array. So, the number of iterations
required to reduce the array size to 1 is approximately log base 2 of n (where n is the initial size of
the array). Therefore, the time complexity grows logarithmically with the input size.
● Binary search: In binary search, the array is divided in half in each iteration to find the
target element.
● Merge sort: Merge sort divides the array into two halves, sorts each half recursively,
and then merges the sorted halves.
● Quick sort: Quick sort partitions the array into two parts based on a pivot element and
recursively sorts each part.
While the exact time complexity might vary slightly depending on the specific implementation
details of the algorithm, the overall trend is O(log n) for algorithms that divide arrays in two parts in
each iteration.