0% found this document useful (0 votes)
66 views8 pages

Sheet 1 With Answers

The document contains 10 questions related to algorithms and data structures. It asks about insertion sort, linear search, binary addition, asymptotic analysis, selection sort, merge sort, and calculating inversions. Pseudocode is provided for algorithms like linear search, selection sort, and calculating inversions using a modified merge sort. Loop invariants and analyses of best/worst cases are discussed for selection sort.

Uploaded by

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

Sheet 1 With Answers

The document contains 10 questions related to algorithms and data structures. It asks about insertion sort, linear search, binary addition, asymptotic analysis, selection sort, merge sort, and calculating inversions. Pseudocode is provided for algorithms like linear search, selection sort, and calculating inversions using a modified merge sort. Loop invariants and analyses of best/worst cases are discussed for selection sort.

Uploaded by

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

Sheet 1

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

Q3) 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.

SEARCH(A, v):
for i = 1 to A.length
if A[i] == v
return i
return NIL

I'm going to state the loop invariant as:


At the start of each iteration of the for loop, the subarray A[1..i−1] consists of elements that
are different than ν.

Here are the three properties:

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

Q5) Express a function n3/1000 – 100n2 – 100n + 3 in terms of Θ- notation.


Θ (max ( n3/1000 ,100n2 ,100n , 3)) = Θ (n3)

Q6)Consider sorting n numbers stored in array A by first finding the smallest


element of A and exchanging it with the element in A[1]. Then find the second
smallest element of A, and exchange it with A[2]. Continue in this manner for
the first n-1 elements of A. Write pseudocode for this algorithm, which is
known as selection sort. What loop invariant does this algorithm maintain?
Why does it need to run for only the first n-1 elements, rather than for all n
elements? Give the best-case and worst-case running times of selection sort
in ‚Θ-notation.
SELECTION-SORT(A):
for i = 1 to A.length - 1
min = i
for j = i + 1 to A.length
if A[j] < A[min]
min = j
temp = A[i]
A[i] = A[min]
A[min] = temp

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].

Why n−1 elements?


In the final step, the algorithm will be left with two elements to compare. It will store the smaller
one in A[n−1] and leave the larger in A[n]. The final one will be the largest element of the array,
since all the previous iteration would have sorted all but the last two elements (the outer loop
invariant). If we do it n times, we will end up with a redundant step that sorts a single-element
subarray.
Running Time
Both are clearly Θ(n2).

Q7) How can we modify almost any algorithm to have a good best-case
running time?

We can modify it to handle the best-case efficiently. For example, if we modify


merge-sort to check if the array is sorted and just return it, the best-case running
time will be Θ(n).
Q8) Illustrate the operation of merge sort on the array
A = (3, 41, 52, 26, 38, 57, 9, 49).
Q9) Rewrite the MERGE procedure so that it does not use sentinels(∞),
instead stopping once either array L or R has had all its elements copied back
to A and then copying the remainder of the other array back into A.

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):

1- declare a left and right arrays

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.

Merge steps (case 2):

1- declare a left and right arrays

2- copy values from A to left and right arrays

3- loop on A array

4-check the end of the left array

5-check the end of the right 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.

a. List the five inversions of the array (2, 3, 8, 6, 1).

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.

d. Give an algorithm that determines the number of inversions in any


permutation on n elements in Θ(n log n) worst-case time. (Hint: Modify merge
sort.)

1. The five inversions


⟨2,1⟩, ⟨3,1⟩, ⟨8,6⟩, ⟨8,1⟩ and ⟨6,1⟩.
2. Array with most inversions
It is the reversed array, that is ⟨n,n−1,…,1⟩. It has (n−1)+(n−2)+⋯+1=n(n−1)2 inversions.

3. Relationship with insertion sort


Insertion sort performs the body of the inner loop once for each inversion. Due to the nature of the
algorithm, on each k-th iteration, if A[1..k] has m inversions with A[k], they are in A[k−m..k−1]
(since the elements before k are sorted). Thus, the inner loop needs to execute its body m times.
This process does not introduce new inversions and each outer loop iteration resolves exactly m
inversions, where m is the distance the element is "pushed towards the front of the array".
Thus, the running time is Θ(n+d), where d is the number of inversions (n comes from the outer
loop).

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

You might also like