-
Insertion Sort
So i havent done java in about two years, its been a while since ive seen arrays. THe assignment is to fix a program that was given to me. I found one error but in the insertion sort part i cant get it to run the while loop. I cant make any fundamental changes to the program, basically i just have to fix the error in the while loop to get it running.
any pointers or advice would be appreciated, just need a kick in the right direction.
below is the program
Code:
import java.io.*; // for BufferedReader
import java.util.*; // for StringTokenizer
public class Prog1Original { // A simple program with no classes
public static void main(String[] args) throws IOException {
int number[] = new int[100];
int ct, num, size, i, j, insel;
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in) );
System.out.print("Input integers: ");
size = 0; // The smallest index of an array is always 0
String inputLine = stdin.readLine(); // All input must be on a single line
StringTokenizer input = new StringTokenizer(inputLine);
while (input.hasMoreTokens()) { // extract the integers from the input line
num = Integer.parseInt(input.nextToken());
number[size] = num;
size = size + 1;
}
System.out.println();
System.out.print("The original numbers: ");
for (ct = 0; ct < size; ct++) {
System.out.print(number[ct]);
System.out.print(" ");
}
System.out.println();
// "Insertion Sort" the numbers
for (i = 1; i < size; i++) { // Starting with the second array element
insel = number[i];
j = i;
while ( (number[j] > insel) && (j >= 0) ) { // shift larger elements right
number[j + 1] = number[j];
j = j - 1;
}
number[j] = insel; // insert the number to its proper place
}
System.out.print("The sorted numbers: "); // Output the sorted array
for (ct = 0; ct < size; ct ++) {
System.out.print(number[ct]);
System.out.print(" ");
}
System.out.println();
}
}
-
So what's wrong with it? Do you get compiler or runtime errors? Then copy and paste the full and exact error message and indicate on which line it occurs. Does it not perform correctly? Then explain what it does and what it should do instead.
-
Also www.physicsforums.com
-----
Hi and welcome to the forums.
A few things to bear in mind when you post here: The first is that you should use the "code" tags. Basically you put [code] at the start of the code and [/code] at the end. This makes the code readable. There is a # button in the panel where you type your post that will add the tags around selected text.
The second is that you should be forthright when crossposting to other sites. It is also a good idea to put a link at each place to the other so that anyone contributing their ideas can see the whole discussion.
Finally although lots of people are happy to help, no-one is going to do your work/homework for you. I'm sure you didn't intend this, but the best way of getting "a kick in the right direction" is to precisely specify the starting point! Ie, say what errors, messages and other undesirable behaviour you are observing with this code.
I hope you don't mind me pointing all this out; my intention is that you get the best help, the most quickly. Such things are pretty standard across "help" fora like these which is why the advice (with regard to formatting and describing the problem) is pretty much what you have recieved at physicsforums.com as well.