Results 1 to 3 of 3
Thread: Insertion Sort
- 08-31-2011, 06:29 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 5
- Rep Power
- 0
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
Java 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(); } }Last edited by pbrockway2; 08-31-2011 at 07:30 AM. Reason: code tags added, and indents replaced
- 08-31-2011, 07:01 AM #2
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.
- 08-31-2011, 07:20 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,548
- Rep Power
- 11
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.Last edited by pbrockway2; 08-31-2011 at 07:40 AM.
Similar Threads
-
Help with insertion sort
By daendoonge in forum Java AppletsReplies: 0Last Post: 01-29-2011, 11:28 PM -
Insertion Sort for linked list help?
By bubtub24 in forum New To JavaReplies: 3Last Post: 11-28-2010, 06:21 AM -
problem with insertion sort???
By blueduiker in forum New To JavaReplies: 2Last Post: 03-22-2010, 01:17 PM -
Insertion Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:41 PM -
Insertion sort algorithm
By Albert in forum Advanced JavaReplies: 2Last Post: 06-28-2007, 08:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks