CSO 221 Solution 1
CSO 221 Solution 1
Ans. In a linear search, the array is scanned from the beginning to the end, from index 1 to index
n, checking if the entry at each position equals v. The pseudocode for this process is as follows:
LINEAR-SEARCH (A, v)
1 for i = 1 to A.length
2 if A[i] == v
3 return i
4 return NIL
LOOP INVARIANT:
At the start of each iteration of the for loop (lines 1-3), the subarray A[1..i-1] does not contain
the value v.
The three necessary properties for the loop invariant hold as follows:
Initialization: Initially, the subarray is empty, so none of its elements are equal to v.
Maintenance: In the i-th iteration, we check whether A[i] is equal to v. If yes, we terminate the
loop; if not, we continue the iteration. Therefore, if the subarray A[1..i-1] did not contain v
before the i-th iteration, it will not contain v before the next iteration (unless the i-th iteration
terminates the loop).
Termination: The loop terminates either when we find v at index i, or when we reach the end of
the array without finding v. In either case, the algorithm behaves as required, making it correct.
2. SelectionSort(A):
n = length(A)
for i from 1 to n - 1:
smallestIndex = i
for j from i + 1 to n:
if A[j] < A[smallestIndex]:
smallestIndex = j
swap A[i] and A[smallestIndex]
The loop invariant that this algorithm maintains is that after the i-th iteration of the outer loop,
the first i elements of array A are in sorted order.
Explanation:
The outer loop runs for the first n - 1 elements because after sorting the first n - 1 elements, the
last element is already in its correct position (since it's the only one remaining).
The inner loop finds the index of the smallest element in the unsorted part of the array, and the
swap operation places that smallest element in its correct position in the sorted part of the array.
The loop invariant holds because at each iteration, the smallest element in the unsorted part is
identified and swapped with the element at the current position of the outer loop.
Best Case: Θ(n^2) - This occurs when the array is sorted in reverse order, and the algorithm has
to make a swap in every iteration of the inner loop.
Worst Case: Θ(n^2) - This occurs when the array is sorted in ascending order, and the algorithm
still needs to make the same number of swaps as in the worst case.
Q.3.
A) O(√n),O(log n^(log n)),O(n^√n), O(n!)
B)
C)
E) How can we modify almost any algorithm to have a good best-case running time?
Ans. We can design any algorithm to treat its best-case scenario as a special case and return a
predetermined solution.
For example, for selection sort, we can check whether the input array is already sorted and if it
is, we can return without doing anything. We can check whether an array is sorted in linear time.
So, selection sort can run with a best-case running time of Θ(n).
Q4 Banks often record transactions on an account in order of the times of the transactions, but
many people like to receive their bank statements with checks listed in order by check number.
People usually write checks in order by check number, and merchants usually cash them with
reasonable dispatch. The problem of converting time-of-transaction ordering to check-number
ordering is therefore the problem of sorting almost-sorted input. Argue that the procedure
INSERTION-SORT would tend to beat the procedure QUICKSORT on this problem. (5
Marks)
Ans The more sorted the array is, the less work insertion sort will do. Namely, INSERTION-SORT is
Θ(n+d), where dd is the number of inversions in the array. In the example above the number of inversions
tends to be small so insertion sort will be close to linear.
On the other hand, if PARTITION does pick a pivot that does not participate in an inversion, it will
produce an empty partition. Since there is a small number of inversions, QUICKSORT is very likely to
produce empty partitions.
Q5 Consider modifying the PARTITION procedure by randomly picking three elements from
array A and partitioning about their median (the middle value of the three elements).
Approximate the probability of getting, at worst, an ⍺ - to - (1 - ⍺) split, as a function of ⍺ in
the range 0 < ⍺ < 1. (5 Marks)
Ans