0% found this document useful (0 votes)
15 views4 pages

CSO 221 Solution 1

The document discusses algorithms for searching and sorting, including linear search and selection sort, along with their loop invariants and running time analyses. It compares Hoare's and Lomuto's partition schemes in QuickSort, highlighting the efficiency of Hoare's method. Additionally, it argues that insertion sort is more effective than QuickSort for almost-sorted input and explores a modification to the partition procedure using a median of three elements.

Uploaded by

agpratham7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

CSO 221 Solution 1

The document discusses algorithms for searching and sorting, including linear search and selection sort, along with their loop invariants and running time analyses. It compares Hoare's and Lomuto's partition schemes in QuickSort, highlighting the efficiency of Hoare's method. Additionally, it argues that insertion sort is more effective than QuickSort for almost-sorted input and explores a modification to the partition procedure using a median of three elements.

Uploaded by

agpratham7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Q1.

Consider the searching problem:


Input: A sequence of n numbers A = {a1 , a2 ,......, an } and a value v.
Output: An index i such that v = A[i] or the special value NIL if v does not appear in A.
Write pseudocode for linear search, which scans through the sequence, looking for v. Using
a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant
fulfills the three necessary properties.

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.

Running Time Analysis:

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)

D) Compare Hoare’s vs. Lomuto's partition scheme in QuickSort.


Ans. Hoare's scheme is more efficient than Lomuto's partition scheme because it does three
times fewer swaps on average. Also, as mentioned, the implementation given creates a balanced
partition even when all values are equal., which Lomuto's scheme does not.

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

You might also like