Sheet 1 With Answers
Sheet 1 With Answers
Q1) Illustrate the operation of INSERTION-SORT on the array A = (31 ,41 , 59, 26, 41, 58).
Q2) Rewrite the INSERTION-SORT procedure to sort into decreasing instead of increasing
order.
for j = 2 to A.length
key = A[j]
i = j-1
while i > 0 and key > A[i]
A[i+1] = A[i]
i= i-1
A[i+1] = key
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.
SEARCH(A, v):
for i = 1 to A.length
if A[i] == v
return i
return NIL
Initialization
Initially the subarray is the empty array, so proving it is trivial.
Maintenance
On each step, we know that A[1..i−1] does not contain ν. We compare it with A[i]. If they are the
same, we return i, which is a correct result. Otherwise, we continue to the next step. We have
already insured that A[A..i−1] does not contain ν and that A[i] is different from ν, so this step
preserves the invariant.
Termination
The loop terminates when i>A.length. Since i increases by 1 and i>A.length, we know that all
the elements in A have been checked and it has been found that ν is not among them. Thus, we
return NIL.
Q4) Consider the problem of adding two n-bit binary integers, stored in two n-
element arrays A and B. The sum of the two integers should be stored in binary
form in an (n+1) -element array C . State the problem formally and write
pseudocode for adding the two integers.
For example:
input
Array A contains :
0 0 1 0 1
Array B contains :
1 0 1 0 1
output
Array C contains :
0 1 1 0 1 0
Answer:
// start index is 1
Carry = 0
For i = n to 1
C[i+1] = (A[i] + B[i] + Carry) mod 2
Carry = (A[i] + B[i] + Carry) / 2
C[1] = Carry
Loop invariants
At the start of each iteration of the outer for loop, the subarray A[1..i−1] contains the
smallest i−1 elements of the array, sorted in nondecreasing order.
And:
At the start of each iteration of the inner for loop, A[min] is the smallest number in the
subarray A[i..j−1].
Q7) How can we modify almost any algorithm to have a good best-case
running time?
MERGE(A, p, q, r)
n1 = q - p + 1
n2 = r - q
let L[1..n₁] and R[1..n₂] be new arrays
for i = 1 to n₁
L[i] = A[p + i - 1]
for j = 1 to n₂
R[j] = A[q + j]
i = 1
j = 1
for k = p to r
if i > n₁
A[k] = R[j]
j = j + 1
else if j > n₂
A[k] = L[i]
i = i + 1
else if L[i] ≤ R[j]
A[k] = L[i]
i = i + 1
else
A[k] = R[j]
j = j + 1
Merge steps (case 1):
2- copy values from A to left and right arrays ( add ∞ at the end of right and
left array)
3- loop on A array
4- for each element compare elements in left and right then write the smallest
element into A.
3- loop on A array
6- for each element compare elements in left and right then write the smallest
element into A.
Q10) Inversions
Let A [1..n] be an array of n distinct numbers. If i < j and A[i] > A[j] , then the
pair ( i, j) is called an inversion of A.
b. What array with elements from the set {1,2 … ,n} has the most inversions?
How many does it have?
c. What is the relationship between the running time of insertion sort and the
number of inversions in the input array? Justify your answer.
4. Calculating inversions
MERGE-SORT(A, p, r):
if p < r
inversions = 0
q = (p + r) / 2
inversions += merge_sort(A, p, q)
inversions += merge_sort(A, q + 1, r)
inversions += merge(A, p, q, r)
return inversions
else
return 0
MERGE(A, p, q, r)
n1 = q - p + 1
n2 = r - q
let L[1..n₁] and R[1..n₂] be new arrays
for i = 1 to n₁
L[i] = A[p + i - 1]
for j = 1 to n₂
R[j] = A[q + j]
i = 1
j = 1
for k = p to r
if i > n₁
A[k] = R[j]
j = j + 1
else if j > n₂
A[k] = L[i]
i = i + 1
else if L[i] ≤ R[j]
A[k] = L[i]
i = i + 1
inversions +=1
else
A[k] = R[j]
j = j + 1
inversions +=1
return inversions