Results 1 to 11 of 11
  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Bubble-sort method

    Hi,

    I've written this method for a program and for some reason everytime I run it I get an error message in the cosule saying something is wrong with the "if" statement (the first one).

    Can't figure it out, the code itself seems just fine and I've got no red lines after writing it.

    Here it is:

    Java Code:
    static void bubbleSort(double[] list)         {
    	    boolean changed = true;
    	    int number_of_passes = list.length - 1;
    	    do {
    	      changed = false;   
    	      double temp;
    		for (int i=0; i<=number_of_passes; i++) {
    		       
    			if (list[i]>list[i+1]) {
    				temp = list[i];
    				list[i] = list[i+1];
    				list[i+1]=temp;		
    
    			}// if ends
    		        
    		
    		}// for ends
    
    	        number_of_passes--;
    	        if (number_of_passes == 0) changed=true;
    	      } while (changed);
    	                                               }
    I'd really appriciate some help :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Bubble-sort method

    I get an error message in the cosule saying something is wrong
    That's strange. Usually the error messages are too specific.
    What did the error message really say?
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  3. #3
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Bubble-sort method

    My list before sort is:
    5.0
    4.4
    1.9
    2.9
    3.4
    2.9
    3.5
    -8.0
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at Try2.bubbleSort(Try2.java:29)
    at Try2.main(Try2.java:11)

    line 29 = line 9 above

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Bubble-sort method

    The error message says that on line 29 the code has an index for an array that is past the end of the array(out of bounds). Look at that line and see now the index got to have a value of 8.
    Add a println just before the line to print out the value of the index so you can see it. Also print out the the length of the array if you are not sure how big it is.

    Remember that array indexes start at 0 and go to the length of the array -1

  5. #5
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Bubble-sort method

    I see now.
    Thanks a lot!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Bubble-sort method

    Ok. That i+1 will get you.

  7. #7
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Bubble-sort method

    Ye,
    It still aint working right, probably because of that i+1 thing.
    But I'll try and figure that one myself at least :)

  8. #8
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Bubble-sort method

    Ok,
    after rewriting the program twice and STILL not finding the problem with it I've surrendered :|

    Here is the method, it's working and I'm getting no error message.
    but it aint doing what it should (well, not completely anyway)..

    Java Code:
     static void bubbleSort(double[] list)         {
    		    boolean changed = true;
    		    int number_of_passes = list.length - 1;
    		    Double temp;
    		    do {
    		      changed = false;   
    		      for(int i = 0; i < number_of_passes ; i++){
    		    	if (list[i]>list[i+1]){
    		    		temp = list[i];
    		    		list[i] = list[i+1];
    		    		list[i+1]=temp;
    		    		
    		    		}//if ends
    		    	  
    		    	  
    		      }//for ends
    		        number_of_passes--;
    		        if (number_of_passes==0) changed=true;
    		      } while (changed);
    		                                               }
    As I've written above, it's a sorting method.
    instead of sorting all the array, it just sorts it for the first element of the array and that's it :( here's the example right from the consule:


    My list before sort is:
    5.0
    4.4
    1.9
    2.9
    3.4
    2.9
    3.5
    -8.0
    My list after sort is:
    4.4
    1.9
    2.9
    3.4
    2.9
    3.5
    -8.0
    5.0

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Bubble-sort method

    You need to try debugging the code by adding printlns to see what the code is doing. Printout the values of the variables as the loops go around to see how the loops are working.
    Print out the values of the variables that are being compared and also the indexes.

    For easier testing reduce the list to be sorted to 2 or 3 elements. This will keep the amount of print outs down.

    Do the sort manually using a piece of paper and a pencil. Compare what you write down in the manual method with what prints out from the program. Add more printlns as you see that you need more data to understand what your code is doing.

  10. #10
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Bubble-sort method

    I feel so stupid right now : \

    It was all about the true and false in the while loop... got messed up.

    Well.. I'm still a noob in programming :( and it aint gonna change anytime soon :(

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Bubble-sort method

    We all make stupid mistakes. Hopefully there will be fewer in the future.

Similar Threads

  1. Bubble Sort For 2D Arrays
    By tmantonym in forum New To Java
    Replies: 2
    Last Post: 11-30-2011, 09:40 PM
  2. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  4. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:42 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 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
  •