tombguy5 Posted November 5, 2013 Share Posted November 5, 2013 Hey there Ahoyworld community! I've been working a Bubble Sort Algorithm for my grade 12 IT class using Java. I have it working properly. However, if I have values like this: 2, 5, 3, 4, 1 the program will sort it as: 1, 2, 3, 4, 5 But, for any values used like this: 111, 90, 12, 555, 3 the program will sort it as: 111, 12, 3, 555, 90 I have been trying to fix this for hours to no avail, does anyone have any idea? Here is the WHOLE code: Also, please let me know how I did everything as it is. I'm sure the error is of something that is extremely simple that I do not even see it! Note: The program is 60 lines long (the challenge for the class is to make it as short as possible for efficiency.) /** * === Bubble Sort Program === * @author Ryan Berezon (528849) */ package bubblesort_v2; import java.util.Scanner; public class Bubblesort_v2 { public static void main(String[] args) throws InterruptedException { int j; Scanner in = new Scanner(System.in); //finding the length of the Array value System.out.print("How many values? - Input in numbers: "); int totalvalue = Integer.parseInt(in.nextLine()); String[] value = new String [totalvalue]; //allows user to input student names int y = 1; for(j = 0; j < value.length;j++) { System.out.print(y + ") Name your values: "); value[j] = in.nextLine(); y += 1; } System.out.println("\n"+"Unsorted List:"); for (String element:value) { System.out.println(element); } Thread.sleep(1500); bubble_srt(value,value.length); System.out.print("\n"+"Sorted List:\n"); for(j = 0; j < value.length; j++) System.out.print(value[j]+" "); System.out.println(); System.out.println("\n" + "PAUSED"); } public static void bubble_srt(String[] a, int n) { int j, i =0; String t; for(j = 0; j < n; j++) { for(i = 1; i < (n-j); i++) { if(a[i-1].compareTo(a)>0) { t = a[i-1]; a[i-1]=a; a=t; } } } } } Please help! Also, feedback to possibly improve the efficiency of the program would be great! Link to comment Share on other sites More sharing options...
Rarek Posted November 5, 2013 Share Posted November 5, 2013 Haven't read the code properly as I'm at work (and I'm insanely jealous you're getting to do programming at school), but generally the problem that's present when sorting behaves like that is that it's sorting strings rather than integers. Looks like you've set the variable value to be a string and that might be where the problem is. Imagine if you were using letters instead of numbers; if we had three sets of letters like so: "afg, ac, aaa", it would be logical to order them like this: "aaa, ac, afg". So the same applies to numbers if they're formatted as a string. If the above string was numbers instead of letters, it'd order it "incorrectly" for our purposes ("111, 13, 167") if comparing strings. So aye, check that everything going/coming in/out is an integer. That might be it. Link to comment Share on other sites More sharing options...
tombguy5 Posted November 5, 2013 Author Share Posted November 5, 2013 I will look at it on that aspect. Thanks Rarek! If I solve it, I'll post the solution here! Cheers! Hmm... Perhaps I should cast it... I'll figure that out P.S. My IT class is actually using Python, but I prefer Java myself so I was able to come to a compromise with my teacher to allow me to use Java for the projects. (Useless bit of information ) Link to comment Share on other sites More sharing options...
tombguy5 Posted November 7, 2013 Author Share Posted November 7, 2013 Well, I tried to cast it, I'm pretty sure I'm on the right track but I cannot seem to fix the error. Here's what I want... I want the program to check if the string value contains integers only so I can cast it to an int rather than a string as it starts off as a string. I think I'm on the right path but I keep getting this error I have no idea how to fix. NOTE: the bold text below is the addition in comparison to my previous code above. CODE: package bubblesort_v2; import java.util.Scanner; import java.lang.*; public class Bubblesort_v2 { public static void main(String[] args) throws InterruptedException {//To solve the string to int format is to cast it int j; Scanner in = new Scanner(System.in); //finding the length of the Array value by user input System.out.print("How many values? - Input in numbers: "); int totalvalue = Integer.parseInt(in.nextLine()); String[] value = new String [totalvalue]; //allows user to input values of any sort. int y = 1; for(j = 0; j < value.length;j++) { System.out.print(y + ") Name your values: "); value[j] = in.nextLine(); y += 1; } isInteger(value,value.length); System.out.println("\n"+"Unsorted List:"); for (String element:value) { System.out.println(element); } Thread.sleep(1500); bubble_srt(value,value.length); System.out.print("\n"+"Sorted List:\n"); for(j = 0; j < value.length; j++) System.out.print(value[j]+" "); System.out.println("\n\n" + "Ending"); } public static void bubble_srt(String[] a, int n) {//String[] a is now a function that allows strings as part of the algorithm same to integer (int n) int j, i =0; String t; for(j = 0; j < n; j++) { for(i = 1; i < (n-j); i++) { if(a[i-1].compareTo(a)>0)//Another way to look at it is: someValue.compareTo(otherValue) and checks if it is positive ("> 0") { t = a[i-1]; a[i-1]=a; a=t; } } } } public static boolean isInteger(String value) { try { Integer.parseInt(value); } catch(NumberFormatException e) { return false; } return true; } } Link to comment Share on other sites More sharing options...
wok Posted November 7, 2013 Share Posted November 7, 2013 Well, I tried to cast it, I'm pretty sure I'm on the right track but I cannot seem to fix the error. Here's what I want... I want the program to check if the string value contains integers only so I can cast it to an int rather than a string as it starts off as a string. I think I'm on the right path but I keep getting this error I have no idea how to fix. NOTE: the bold text below is the addition in comparison to my previous code above. CODE: At first glance I can tell you that when you are calling isInteger you are using 2 parameters (value, value.length), but where you define it, it only has one parameter. Also I think you are passing a String[] and in the definition the parameter is a String. I will try to take a deeper look at the code later if I get some time. UPDATE I took a closer look, and in order to make it work you should modify the bubble_str() function, instead of comparing strings you need to compare ints, just replace this: if(a[i-1].compareTo(a[i])>0) with: if( Integer.parseInt( a[i-1] ) > Integer.parseInt( a[i] ) ) I commented the isInteger thing to be able to compile the code. I tested it with the input numbers 111, 90, 12, 555, 3 and the output was 3, 12, 90, 111, 555. In order to make the isInteger thing work you probably would want to use it inside the loops on bubble_str(), something like: if( !isInteger( a[i] ) ) continue; But since you have 2 nested loops you probably need to use it once on each loop. There are many other ways to do this, you could check the user input to only accept ints so you show them some warning and dont accept their input when its not numbers only, you can cleanup the array before using it to generate one that only contains only ints, etc. tombguy5 1 Link to comment Share on other sites More sharing options...
tombguy5 Posted November 7, 2013 Author Share Posted November 7, 2013 Awesome, I'll figure out how to get a string to work because the assignment requires me to accept both integers and string to bubblesort. Lots of Props to you Wok! Link to comment Share on other sites More sharing options...
Rarek Posted November 7, 2013 Share Posted November 7, 2013 Woohoo! I was right! *dances on the tables* tombguy5 1 Link to comment Share on other sites More sharing options...
danne Posted November 7, 2013 Share Posted November 7, 2013 Wok and Rarek: you are great guys. :-) I suggested asking in the ahoy forums the first time berezon needed help with java and I couldn't help him. It worked then and it worked this time too. Makes me feel good about the community :-) tombguy5 1 Link to comment Share on other sites More sharing options...
tombguy5 Posted November 8, 2013 Author Share Posted November 8, 2013 Wok and Rarek: you are great guys. :-)I suggested asking in the ahoy forums the first time berezon needed help with java and I couldn't help him. It worked then and it worked this time too. Makes me feel good about the community :-) It's true, danne suggested this for any assistance and I got it, thanks to you danne. And many thanks to Wok and Rarek for suggesting and helping me! Link to comment Share on other sites More sharing options...
tombguy5 Posted November 16, 2013 Author Share Posted November 16, 2013 Okay, I have found the solution! I had to use a try and catch statement in the bubblesort algorithm with an integer parse and and a NumberFormatException for the catch statement with a regular string comparison. So now this program will run on both Integer and String datatypes at once in the program. CODE: package bubblesort_v2; import java.util.Scanner; import java.lang.*; public class Bubblesort_v2 { public static void main(String[] args) throws InterruptedException { int j; Scanner in = new Scanner(System.in); //finding the length of the Array value by user input System.out.print("How many values? - Input in numbers: "); int totalvalue = Integer.parseInt(in.nextLine()); String[] value = new String [totalvalue]; int y = 1; for(j = 0; j < value.length;j++) //allows user to input values of any sort. { System.out.print(y + ") Name your values: "); value[j] = in.nextLine(); y += 1; } Thread.sleep(2000); System.out.println("\n"+"Unsorted List:"); for (String element:value) { System.out.println(element); } Thread.sleep(1500); bubble_srt(value,value.length); System.out.print("\n"+"Sorted List:\n"); for(j = 0; j < value.length; j++) System.out.print(value[j]+" "); System.out.println("\n\n" + "Ending"); } public static void bubble_srt( String[] a, int n ) {//String[] a is now a function that allows strings as part of the algorithm same to integer (int n) int j, i = 0; String t; for(j = 0; j < n; j++) { for(i = 1; i < (n-j); i++) { try { if( Integer.parseInt(a[i-1]) > Integer.parseInt(a) ) { t = a[i-1]; a[i-1]=a; a=t; } } catch( NumberFormatException E ) { if( a[i-1].compareTo(a)>0 ) //Another way to look at it is: someValue.compareTo(otherValue) and checks if it is positive ("> 0") { t = a[i-1]; a[i-1]=a; a=t; } } } } } } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now