1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Insertion sort problem

  1. Aug 30, 2011 #1
    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

    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.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;
    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(" ");
  2. jcsd
  3. Aug 31, 2011 #2


    Staff: Mentor

    I added (code) and (/code) tags (but in square brackets) to preserve your original formatting.
    Can you give more details about what you mean that you can't get it to run the while loop. If a while loop isn't executing, it's because the test condition at the top of the loop is false. That means that number[j] <= insel or j < 0, or both.
  4. Aug 31, 2011 #3
    i put in System.out.print(" " + j); into the while loop to see if it would print out anything while doing the loop. my thinking was that if it did then the program goes through the loop but it didnt print that out which leads me to think that it doesnt go through the loop.
  5. Aug 31, 2011 #4


    Staff: Mentor

    Right. If you don't have a debugger to work with, or do have one but don't know how to use it, put a print statement just before the start of the loop to display the values of j and number[j].

    If you have a debugger available, it would be a very good idea to learn how to use it.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook