Results 1 to 2 of 2
  1. #1
    JohnF8FJohn is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default InsertionSort My SourceCode - wrong results

    I've writte an InsertionSort
    It runs so far, but it delivers the wrong results so the code for the sorting must have a mistake. code lines 22-33 must not be working accordingly, but i dont see whats wrong

    Java Code:
    import java.util.Scanner;
    
    public class sort4 {
       public static void main(String args[]) {
    
          Scanner nutzerEingabe = new Scanner(System.in);
          System.out.print("InsertionSort: Enter a number ");
          int zahl = nutzerEingabe.nextInt();   
          
          String x = ""+zahl; //String for length
             int len = x.length();//of the number
             int [] a = new int[len];// Create array with that length
             
             // Get number into array
             for(int k=1;k<len;k++){
                int ziffer =zahl%10;
                zahl = zahl/10;
                a[k]=ziffer;
             }
            // Insertion Sort starts
             int z,y,compareX=0;
          for (int i=1; i<len; i++) {
             compareX++;    // counts comparisons
             z = a[i];      //Int-value at position i
             y = a[i-1];      //Int-value left of it
             while (y>=0 && y > z){// if left side is bigger than z
                z = a[i-1];   // exchange them
                
                y--;
             }y = a[i];
          }/[b]
          for (int l=0;l<len;l++){// output
          System.out.print(""+a[l]);
          }
          System.out.println("\tresult after "+compareX+" comparisons");
       }
    }
    // Insertion Sort starts
    int z,y,compareX=0;
    for (int i=1; i<len; i++) {
    compareX++; // counts comparisons
    z = a[i]; //Int-value at position i
    y = a[i-1]; //Int-value left of it
    while (y>=0 && y > z){// if left side is bigger than z
    z = a[i-1]; // exchange them

    y--;
    }y = a[i];

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    • To begin with, line 33 won't let it compile:

    Java Code:
    }/[b] <-makes compiler upchuck
    • Another observation would be the following piece of code:

    Java Code:
    for(int k=1;k<len;k++){
                int ziffer =zahl%10;
                zahl = zahl/10;
                a[k]=ziffer;
    Not sure what you're trying to do here, but is zahl in the right position?... Should the zahl assignment before the ziffer assignment?
    • Line 27: If you are going to swap the y-z values, shouldn't the following assigment:
    Java Code:
    z = a[i-1];   // exchange them
    be like this:
    Java Code:
    a[i-1] = z;   // exchange them
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. how to take query results to a jlist!!
    By themburu in forum JDBC
    Replies: 1
    Last Post: 06-07-2008, 10:51 AM
  2. how to take query results to a jlist!!
    By themburu in forum New To Java
    Replies: 3
    Last Post: 06-07-2008, 10:45 AM
  3. showing results in a for loop randomly
    By vexity in forum New To Java
    Replies: 4
    Last Post: 04-29-2008, 04:24 AM
  4. date and calender not getting the right results
    By valoyivd in forum New To Java
    Replies: 4
    Last Post: 04-14-2008, 11:51 AM
  5. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 04:07 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
  •