Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this assignment, you should devise an algorithm to solve a simple array problem and argue that your algorithm is correct. You should not execute
In this assignment, you should devise an algorithm to solve a simple array problem and argue that your algorithm is correct. You should not execute or debug your algorithm on a computer. The purpose of this exercise is to practice creating a correct algorithm by reasoning rather than by trial-and-error experimentation. The goal is not just to come up with some code that you think might be correct, but that you know is correct. (Hint: imagine you are in front of a whiteboard trying to present your algorithm to a colleague (or to a person interviewing you!) and convince them that it works.) This is probably quite different than your usual experience with programming and may seem more difficult than just "trying stuff" on the computer. You should not spend unreasonable amounts of time on it (a couple of hours should be more than enough). If you haven't come up with a full solution after a couple of hours, write up a description of what you have figured out and why you believe it should work (or why not!), The goal is to start to think about programming in a more analytic way, and even if you don't get a completely correct solution you still will get the intended benefit from the exercise and will receive credit if you put in the effort to create a reasonable solution. Over the next week or two we will introduce techniques for thinking about these kinds of problems to make them much easier to solve. Doing this exercise now will help us understand the issues involved and see later why these techniques are so valuable. Requirements The input to your algorithm is an array b containing n integer values in locations b[0] to b[n-1]. You may assume that n >= 1. Your algorithm should rearrange the elements of the array so that all the negative (0) elements. You may only modify the array using swap operations that switch (interchange) the values in two elements of the array. To simplify your code, you can write swap (b[i],b[i]) as shorthand for performing the assignments necessary to replace the value in b[i] with that in b[i] and vice versa. You may not create any additional arrays, dictionaries, or other collection data structures to hold copies of the original data. You will, of course, want to create some scalar variables to hold various values as part of your algorithm. Your algorithm should run in O(n) time. You should try to solve the problem using one pass through the array if you can. You may not execute or debug your algorithm on a computer. Write Up The write up of your solution should contain the following parts: 1. Your name. 2. Your algorithm, written in Java syntax. This should be a single Java method rather than an entire class or program. Also, keep in mind that this code is to be read by a human, not a compiler, so don't stress out about minor syntax issues. Instead, focus on writing code that will be easy for others to read. 3. An argument that your algorithm is correct. Your argument should be aimed at convincing a person that is skeptical about its correctness. Explain, in small steps that they cannot disagree with, why the output produced by your algorithm will always be correct. Your argument should not just describe what the code does in English (e.g., "we go through the array and for each item we ..."). You should assume the reader understands Java code and can see what it does. Your goal is to convince them that performing these steps always produces a correct solution to the
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started