Results 1 to 2 of 2
- 11-26-2008, 11:57 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 1
- Rep Power
- 0
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
// Insertion Sort startsJava 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"); } }
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];
- 11-27-2008, 01:11 AM #2
- 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:
Not sure what you're trying to do here, but is zahl in the right position?... Should the zahl assignment before the ziffer assignment?Java Code:for(int k=1;k<len;k++){ int ziffer =zahl%10; zahl = zahl/10; a[k]=ziffer;
- Line 27: If you are going to swap the y-z values, shouldn't the following assigment:
be like this:Java Code:z = a[i-1]; // exchange them
Luck,Java Code:a[i-1] = z; // exchange them
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
how to take query results to a jlist!!
By themburu in forum JDBCReplies: 1Last Post: 06-07-2008, 10:51 AM -
how to take query results to a jlist!!
By themburu in forum New To JavaReplies: 3Last Post: 06-07-2008, 10:45 AM -
showing results in a for loop randomly
By vexity in forum New To JavaReplies: 4Last Post: 04-29-2008, 04:24 AM -
date and calender not getting the right results
By valoyivd in forum New To JavaReplies: 4Last Post: 04-14-2008, 11:51 AM -
BigInteger remainder results in zero
By perito in forum New To JavaReplies: 1Last Post: 03-21-2008, 04:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks