Homework 1 : Designing and Writing about an Algorithm
Objective
Table Of Contents
Problem
Write an algorithm to partition the elements of an integer array as described below and argue that your algorithm is correct. You should not run your algorithm or debug it on a computer. Please use Java syntax for your code, but do not write an entire program; just one method with the correct behavior. Because you are not using a computer we will not grade on tiny syntax mistakes like forgetting a semicolon, but do your best to write a clear and correct algorithm.
Your algorithm should work as follows:
- The input is an array b containing
n
integer values in locationsb[0]
tob[n-1]
. You may assume thatn >= 1
. - Your algorithm should rearrange the original elements of the array so that all the negative elements in the original array, if any, appear at the front of the array starting in location
b[0]
; all of the 0 elements in the original array, if any, are moved to the middle; and all the positive elements, if any, follow that up to positionb[n-1]
. The array does not need to be sorted, just partitioned so the negative, zero, and positive elements are grouped together. - Use assignment statements to copy or swap the original values as needed. You may write
swap(x,y)
as a shorthand for interchanging the contents of variablesx
andy
(including array elements likeb[i]
). You may not use arithmetic or logical operations to compute new integer values to store in the array, and you may not assign integer constant values (like 0) to array elements. The idea is to rearrange the contents of the original array only by swapping array elements. - You may use a small number of additional integer variables in your solution but no additional arrays.
- Your solution should run in \(O(n)\) time, preferably without multiple passes through the array.
Submission
Submit a paper copy of the following at the beginning of class on the due date:
- Your algorithm.
- An argument that your algorithm is correct.
- An argument that your algorithm takes \(O(n)\) time.
You may use any informal, but precise, style you wish for your arguments. Be brief. Clarity in presentation is tantamount in this course.
If you type your solution, you may use any format you like, e.g. plain text, LaTeX, Word, etc. We will use Markdown formatting during the semester, so this would also be a good time to start learning how to format with it. There are a number of good Markdown editors available, including Atom and MacDown. Both of these are free to download. Our lab machines all have Atom already installed. If you do use Markdown, here is a simple template file that you may use to get started.
Grading Criteria
All written homework questions are graded out of five points, roughly as follows:
- 5: The solution is clear and correct. This solution would easily find a home in a textbook on the subject.
- 4: The solution contains a few minor mistakes, but they are of little significance overall.
- 3: The solution hits on the main points, but has at least one major gap in correctness or explanation.
- 2: The solution contains several mistakes, but parts of it are salvageable.
- 1: The solution misses the core concepts of the question.
- 0: No attempt is made at solving the problem.