PDA

View Full Version : Beginners Java Assignment Help

zechmann
Nov 23, 2007, 12:40 AM
error

overanalyzer
Nov 23, 2007, 12:45 AM
Do you mean algorithm approaches?

Wikipedia has a decent list of some approaches
http://en.wikipedia.org/wiki/Sorting_algorithm

And I found these code samples of various approaches via Google
http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html

robbieduncan
Nov 23, 2007, 02:35 AM
The simplest way to sort an array in Java is to use one of the sort methods in the Arrays class (http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html). If your book does not tell you that then there is something seriously wrong.

Jasonbot
Nov 23, 2007, 05:27 AM
Bubble sort (http://www.cs.princeton.edu/~ah/alg_anim/gawain-4.0/BubbleSort.html)

garethlewis2
Nov 23, 2007, 09:40 AM

zechmann
Nov 25, 2007, 07:37 PM
error

Jasonbot
Nov 25, 2007, 11:04 PM
heres what i got so far... i dont quite have arrays down so far so thats whats my problem right now... i haven't got to sorting it out yet but i am first trying to generate n amount of numbers between 0 - 100... what am i doing wrong?

import java.io.*;
public class Sorting
{
public static void main(String[] args)
throws java.io.IOException
{

String h;
int i;

do
{
System.out.print("How many numbers am I gonna sort? ");

i = Integer.parseInt(h);
}
while(i >= 10000 || (i % 2.0) == 1);

int order[] = new int[i];
order = (int[]) (Math.random() * 101);

System.out.println(order);
}
}

Your array isn't even in the loop. Create a for loop using I as a counter then put order[i]=(int)(Math.random()*101); inside the for loop. This will create and put your 100 random numbers in your array. As a beginner coder myself I like your input stream reader

zechmann
Nov 25, 2007, 11:12 PM
error

Gelfin
Nov 26, 2007, 12:54 AM
A few notes...

1. Are you sure you are not allowed to use the built-in array sort method (https://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(int[],%20int,%20int)) to accomplish this? If you're in an introductory Java class and you walk in having implemented your own bubble sort when that isn't a requirement, your instructor is going to suspect you've plagiarized some code from somewhere.

2. Is it a requirement of your assignment that the length of your array is an even number under 10000? I only ask because it's what your code does, and you didn't mention it.

3. Your input value test accepts zero or negative numbers. Since these do not make sense for an array length, it probably isn't what you want.

4. If your user puts in an invalid value, he just sees the same prompt again, with no indication why. Provide more information upfront about the restrictions on possible input values, and if you can, tell the user that his input was not valid before prompting him again.

5. Your prompt is worded using a very casual tone. Some instructors will care. Later on, all employers will care. Be formal.

6. You printed your original array, albeit with linebreaks between numbers, but then later you said you don't know how to print your sorted array. This confuses me. What difference do you think is relevant?

zechmann
Nov 26, 2007, 01:20 AM
error

Gelfin
Nov 26, 2007, 11:27 AM
Clarifying the assignment fixes most of my concerns except:

1. Your prompt says "gonna" where the specified prompt says "going to". If I were your very strict professor, I'd dock points for that.

2. Your code accepts negative numbers, which the specification forbids, and rejects positive odd numbers, which the specification does not mention. Have the painkillers led you to confuse "positive" with "even"? Understandable in a way, but you should fix this.

BONUS: Don't worry about this if you're short on time, because the specification doesn't say to explicitly, but if you have time to go for suckup points, when the user inputs something invalid tell him why it's invalid before displaying the specified prompt again.

I don't see anything immediately wrong with your bubble sort, but I haven't run it or checked it thoroughly. I'm just assuming it works. You shouldn't.

Conceptually, a bubble sort is more or less the simplest kind of sort you can do. It does several passes over the array. There isn't any need to break the algorithm up this way in implementation, but it makes more sense to think of the inner loop as its own function, "pushBiggestNumberToEnd." That's all it does, and it does it by checking each number against its immediate neighbor to the right. The bigger number always ends up to the right of the smaller number. In this way the big number floats out to the position you want it in.

The outer loop simply calls pushBiggestNumberToEnd on a shrinking subset of the entire array. The first pass operates on the entire array, [0..n]. Once the first pass is done, you know the last number in the array (at n) is in the right position, so you can ignore it.

The next pass calls pushBiggestNumberToEnd on the subset of the array [0..n-1]. This has the effect of pushing the second biggest number in the array to the position n-1. This is the right position for that number, so the outer loop now runs on [0..n-2] and so forth until it has sorted the entire array.

Selection Sort is another simple sort algorithm that works sort of like an inside-out version of bubble sort. The inner "function" in the selection sort would be "swapSmallestWithFirst." This function examines each element in an array (or portion of an array) and keeps track of the location of the smallest number it encounters. When it reaches the end of the array, it swaps the number at the remembered location with the number at the first location in the array. At the end of a pass, you know that the first number in the portion of the array you're working on is the smallest number in the entire portion.

Thus the outer loop in Selection Sort behaves like in Bubble Sort, except it shrinks at the small end instead of the big end. It puts the correct number into position 0, then position 1 and so forth until it has sorted the entire array.

Jasonbot
Nov 26, 2007, 12:21 PM
//

import java.util.*;

public class srt {

public static void main (String args[]) {
int a [] ={12,3,4,345,123,13,45,123123,43,1};
int x;

str.sort(a[1]);

do{
i++;
System.out.println( a [i]);
x= a[i];
}while (x!=null);

}
public static void sort(int[] a, int a[0], int a[10]){

}
}

sorry to interrupt the thread, where is my sort messing up?

Gelfin
Nov 26, 2007, 12:48 PM
sorry to interrupt the thread, where is my sort messing up?

You just need to check your basic Java syntax. I'm not even exactly sure what you're trying to accomplish with some of this.