Vous êtes sur la page 1sur 2

I500: Fundamental Computer Concepts of Informatics

HW1 (Due: Sep. 12 Friday BEFORE Lab session)


http://darwin.informatics.indiana.edu/col/courses/I500-14

1. (10 pts) Illustrate the operation of Insertion-sort algorithm on array
A=<31,41,59,26,41,58>.

2. (10 pts) The input to the algorithm Unknown illustrated below is an array A of N
numbers. (1) what is the output of the algorithm? (2) using big-O notation to show
the running time of the algorithm.

Input: Array A of N numbers;
Unknown(A)
for j = 1 to N-1
if A[N] < A[j]
exchange A[j] and A[N]
Output A[N];

3. (Analysis of selection-sort algorithm, 10 pts)

Selection-sort is an algorithm to sort a given list of numbers. It works as follows:
1. Find the minimum value in the list;
2. Swap it with the value in the first position;
3. Repeat the steps above for the remainder of the list (starting at the second position and advancing
each time)

SELECTI0N-S0RT(A)
n = A:length
foi j = 1 to n-1
smallest = j
foi i = j+1 to n
if A|ij < A|smallestj
smallest = i
exchange A|jj with A|smallestj

What is the worst case and best case running time of selection-sort? How does it
compare to insertion sort?

4. (10 pts) An array A[1..n] contains all integers from 0 to n except one number. It
would be easy to determine to the missing number by using an auxiliary array
B[0..n] to record which numbers appear in A. Here, we want to avoid the
additional storage of B with the size O(n). Devise an algorithm to determine the
missing integer in O(n) time under this constraint. (Note, you can still use
additional constant memory as temporary storage.)

5. (20 pts) 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 inversions in the input array?
Justify your answer.

6. (20 pts _+ 5 pt bonus) Let F
n
be the nth Fibonacci number. F
n
can be computed in
the matrix form:

F
1
F
2
!
"
#
$
%
&
=
0 1
1 1
!
"
#
$
%
&'
F
0
F
1
!
"
#
$
%
&

F
2
F
3
!
"
#
$
%
&
=
0 1
1 1
!
"
#
$
%
&'
F
1
F
2
!
"
#
$
%
&
=
0 1
1 1
!
"
#
$
%
&
2
'
F
0
F
1
!
"
#
$
%
&


F
n
F
n+1
!
"
#
$
%
&
=
0 1
1 1
!
"
#
$
%
&
n
'
F
0
F
1
!
"
#
$
%
&


So, in order to compute F
n
, it suffices to rate the 2!2 matrix (denoted as X) to the nth
power.

(1) (5 pts) Show that the two 2!2 matrices can be multiplied using four additions and
8 multiplications;
(2) (10 pts) Devise an algorithm to compute X
n
that takes O(log n) times of matrix
multiplication;

It seems we have an algorithm that needs only O(log n) time to compute the Fibonacci
number. However, the catch is that the algorithm involves multiplication, not just
addition; and the multiplication of large numbers is slower than addition.

(3) (5 pts) Let M(n) be the running time of an algorithm of multiplying n-bit numbers.
Show that the run time of the new algorithm is O(M(n) log n).
(4) (bonus 5 pts) Can you show the run time of the algorithm is O(M(n)), if
M(n)=O(n
a
) for some 1"a"2 (Hint: the bit-length of the numbers being multiplied
get doubled with every squaring).

7. (20 pts) Consider the search problem:
Input: a sequence of n numbers <a
1
, a
2
, , a
n
> 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 a linear time search, which scans through the sequence,
looking for v. Use loop invariant statements to show your algorithm is correct.

Vous aimerez peut-être aussi