Results 1 to 3 of 3

Thread: Insertion Sort

  1. #1
    apiwowar is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default 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*;    // 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.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.print("The original numbers: ");
          for (ct = 0; ct < size; ct++) {
             System.out.print("  ");
           // "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("  ");
    Last edited by pbrockway2; 08-31-2011 at 07:30 AM. Reason: code tags added, and indents replaced

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power


    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power




    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 as well.
    Last edited by pbrockway2; 08-31-2011 at 07:40 AM.

Similar Threads

  1. Help with insertion sort
    By daendoonge in forum Java Applets
    Replies: 0
    Last Post: 01-29-2011, 11:28 PM
  2. Insertion Sort for linked list help?
    By bubtub24 in forum New To Java
    Replies: 3
    Last Post: 11-28-2010, 06:21 AM
  3. problem with insertion sort???
    By blueduiker in forum New To Java
    Replies: 2
    Last Post: 03-22-2010, 01:17 PM
  4. Insertion Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:41 PM
  5. Insertion sort algorithm
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 08:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts