Download HW04 due 10/29

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

Array data structure wikipedia , lookup

Transcript
CmSc250 Algorithms
Homework 04 due 10/29
Purpose:
a)
b)
c)
d)
Practice solving recurrence relations, Big Oh problems
Analyze iterative methods
Practice proofs
Design an efficient algorithm
Problems 1, 2, 3, 4, 5 are individual work, problem 6 is teamwork
1. Write the recurrence relation for the best case in Quicksort and solve it. (10 pts)
2. Solve the following recurrence relations: (15 pts)
T(N) = 2T(N/2) + N2
3. Give an analysis of the running time for the following loops, using the Big-Oh
notation. Explain your reasoning (5 pts)
for( i = 1; i < n-1; i=2*i)
if(2*i < n && a[i] > a[2*i])
{
temp = a[i];
a[i] = a[2*i];
a[2*i] = temp;
}
4. What does the following code do? What is the meaning of the returned value?
Give an analysis in terms of Big Oh. (15 pts)
public static int methodOne(int [] array)
{
int i, j;
int n = array.length;
i = 0;
while (array[i]!= 0 && i < n) i++;
if (i == n);
else
for (j = i + 1;j < n; j++)
if (a[j] !=0) a[i++] = a[j];
return i;
}
1
5. Prove the following equality (20 pts)
∑
2(h – i)2i = 2(N – log2(N+1)) where N = 2(h+1) - 1
i = 0 to h-1
Note: You have to write explicitly each step of the transformations and all
transformations have to be correct.
Teamwork
6. Design and implement a linear algorithm (O(N) run time) that rearranges the
elements in an array of size N filled with positive and negative integers so that all
negative integers precede all positive integers. Assume that there are no zero
elements.
Some guidelines:
Implement the algorithm as a method
In the main method, initialize an array of size at least 10 with negative and
positive integers.
Print the array after the elements have been rearranged.
Test the algorithm with
a. array randomly filled with negative and positive numbers
b. array where the negative numbers precede the positive numbers
c. array with all negative numbers
d. array with all positive numbers
e. array that starts with a negative number
f. array that starts with a positive number
g. array that ends with a negative number
h. array that end with a positive number
i. array of size 1
j. array of size 2 that meets the conditions (c) through (h) above
This problem is worth 35 pts (30 for the algorithm and 5 for the program
layout with comments). If the algorithm is not linear, it receives 15 points. If the program
does not work on some test examples, the algorithm will receive partial credit, with 0
points if none of the test examples are solved correctly
Describe the algorithm in the top comment area of the program.
2
Guidelines how to split the work: After you have discussed the problem and designed the
algorithm collectively, one team member implements the algorithm, the other team
member implements the main method. Split the test examples when testing the program.
How to submit:
Solve the individual problems in a word document (you can download and use this
document) named
<your first name>HW04<optional text as in the name of the assignment>
Create a folder that has the same name as the word document.
Save in that folder the word document and the source code of the Java program.
Zip the folder and submit on Scholar.
3