How can I fix an error in the insertion sort while loop in a Java program?

  • Context: Comp Sci 
  • Thread starter Thread starter apiwowar
  • Start date Start date
  • Tags Tags
    Java Sort
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting an error in the insertion sort implementation of a Java program. Participants are focused on identifying why the while loop responsible for sorting is not executing as expected, without making fundamental changes to the existing code.

Discussion Character

  • Technical explanation
  • Debugging assistance

Main Points Raised

  • One participant expresses difficulty in getting the while loop in the insertion sort to run, indicating they have already identified one error but need help with the loop.
  • Another participant suggests that if the while loop is not executing, it could be due to the test condition being false, specifically that either number[j] is less than or equal to insel or j is less than 0.
  • A participant mentions using a print statement within the while loop to check if it executes, noting that no output suggests the loop is not entered.
  • Another reply recommends placing a print statement before the loop to display the values of j and number[j] for further debugging insights.
  • There is a suggestion to utilize a debugger if available, highlighting the importance of understanding how to use debugging tools effectively.

Areas of Agreement / Disagreement

Participants generally agree on the need to investigate the conditions preventing the while loop from executing, but no consensus on the specific solution or cause of the issue has been reached.

Contextual Notes

Participants have not resolved the underlying assumptions about the input data or the initial state of the variables, which may affect the execution of the while loop.

apiwowar
Messages
94
Reaction score
0
So i haven't done java in about two years, its been a while since I've 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 can't get it to run the while loop. I can't 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.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;
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();
}
}
 
Physics news on Phys.org
I added (code) and (/code) tags (but in square brackets) to preserve your original formatting.
apiwowar said:
So i haven't done java in about two years, its been a while since I've 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 can't get it to run the while loop. I can't 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();
   }
}

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.
 
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 doesn't go through the loop.
 
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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
18K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K