Download HW 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Elementary mathematics wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Line (geometry) wikipedia , lookup

Transcript
1
COMPSCI 321-001 Data Structures (Spring 2015)
Homework #1 (80 points), 2/24/2014 (Tuesday)
• Q1(10 points): Asymptotic Notations
(a)(3 points) Which one of the following is wrong?
1.
2.
3.
4.
Θ(n) + O(n) = Ω(n)
Θ(n) + O(n) = O(n)
O(n) + Ω(n) = O(n)
f (n) = o(g(n)) implies g(n) = Ω(f (n))
(b)(3 points) Which one of the following sorting algorithms will have the worst best-case
running time?
1.
2.
3.
4.
Selection sort
Insertion sort
Heap sort
Quick sort
(c)(4 points) Explain why the statement, “The running time of an algorithm is at most
Ω(n),” is meaningless.
2
• Q2(12 points): Divide-and-Conquer
A 3-way merge-sort is an algorithm to sort an array A, described as follows:
Divide: divide the array into three (about) equal sized subarrays A[p..q1], A[q1+1..q2] and
A[q2+1..r]
Conquer: Recursively sort these three subarrays.
Combine: Call a procedure 3-way-merge(A, p, q1, q2, r) to merge three sorted subarrays to a sorted array
(a)(8 points) Please write a recursive pseudocode for this algorithm. Assume the 3-way-merge
procedure is available to be used (you can call it directly).
3-way-merge-sort(A, p, r)
// A: the input array,
// r: ending index.
p: starting index,
{
}
(b)(4 points) Please write down the recurrence equation for this 3-way-merge-sort algorithm.
3
• Q3(12 points): Divide-and-Conquer
Suppose that a computer does not know how to compute the value C2n directly if n > 2, but
the computer can return C22 = 1 in constant time. Also, the computer takes only constant
time for scalar arithmetic operations. We can use divide and Conquer technique to compute
C2n as follows.
(
C2n
=
1
if n = 2
n/2
2
2C2 + (n/2) if n = 2k , where k > 1 is a positive integer
(a)(8 points) Please write down the three steps Divide, Conquer and Combine to describe
how the computer calculates C2n .
(b)(4 points) Please write down the running time recurrence if C2n is computed using the
above approach.
4
• Q4(28 points): Sorting
(a)(5 points) For a given input array A : < 2, 6a , 3, 1, 5, 4, 6b , 9, 8 >, what is the sequence of
numbers in A after calling Build-Max-Heap(A) ? (please show the intermediate trees).
(b)(5 points) For a given input array A : < 6, 2a , 8, 1, 9, 2b , 7, 3, 5 >, what is the sequence of
numbers in A after the first partition (by calling Partition(A, 1, 9))? Note that 1
and 9 in Partition(A, 1, 9) function call are array indexes.
5
(c)(8 points) By using the Max-Heap data structure to implement a priority queue, some
applications may need to change the data (priority) of a specific node i. That is, given
an index i, change the priority of node i to a new priority t. Please write a pseudocode
for this procedure.
Max-Heap-update(A, i, t)
{
}
(d)(10 points) Given an array A, we try to sort the array using the quicksort algorithm.
In this sorting, assume on each recursive step the partition procedure always partition
each subarray into n − 2 to 2 ratio, where n is the size of the subarray. What’s the
running time Θ-notation of this sorting? Please justify your answer.
6
• Q5(18 points): Linear Time Sorting
(a)(4 points) Please describe the reason(s) why we choose the counting sort algorithm to
sort each digit in the Radix Sort?
(b)(6 points) What is the best running time to sort n integers in the range [0, n3 − 1], and
How?
7
(c)(8 points) Given an input array A with n integers in [0, k], we can use the array C in the
counting sort to find out how many integers in A are in a range [a, b]. Write a pseudocode
for this query. Assume C[i] already contains the number of input integers ≤ i.
FindNumIn(a, b) // both a and b are integers