Results 1 to 7 of 7

Thread: Bubble sort

  1. #1
    jdubya is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default Bubble sort

    This is my first java project. I have experience in C++ and I'm been trying to convert C code to Java. Here's what I have.

    Java Code:
    // BubbleSort Java
    // compile with:  javac BubbleSort.java
    // run with: java BubbleSort
    
    import java.util.Arrays;
    
    public class BubbleSort {
        
        public static void main(String args[]) {
            int swapHolder;
            int sizeOfArray = 10; 
            int pass = 0;
            int[] myArray = {44,9,237,1,5,34,534,17,21,819};
            
            while(pass < sizeOfArray) {
                for(int j=0; j < sizeOfArray - pass; j++) {
    	       if(myArray[j] > myArray[j+1]) { // needs to be swapped
    	          swapHolder = myArray[j+1];
    	          myArray[j+1] = myArray[j]; // this makes both the same
    		  myArray[j] = swapHolder;
    	       }
    	     } // end of pass
             pass++;
             }
             //**first pass puts greatest number in last index**
             //cout << "The array sorted from lowest to highest:" << endl;
             System.out.printf("The array sorted from lowest to highest:\n");
             for(int j=0; j < sizeOfArray; j++){
                //cout << myArray[i] << " " ;
                System.out.printf(myArray[j] + " ");
             }
        }  
    }
    It compiles but when I execute I get an error. This one:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at BubbleSort.main(BubbleSort.java:17)

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Bubble sort

    Array index starts from 0. So, your array of 10 is 0 - 9; 10 is an invalid index and that happens in line 17, you try to access the index of 10

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default Re: Bubble sort

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at BubbleSort.main(BubbleSort.java:17)
    At line 17 the code used an index in an array that was past the end of the array. Look at that line and find the index that was past the end. The array has less than 11 elements.
    Remember that array indexes range from 0 to the array length -1.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jdubya is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default Re: Bubble sort

    Quote Originally Posted by Norm View Post
    At line 17 the code used an index in an array that was past the end of the array. Look at that line and find the index that was past the end. The array has less than 11 elements.
    Remember that array indexes range from 0 to the array length -1.
    Does the subtraction not get done here? line16: sizeOfArray - pass This worked in my C++ code.

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Bubble sort

    The subtraction is done fine.

    Initialize the pass variable to 1 instead of 0 and see what happens.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,029
    Rep Power
    6

    Default Re: Bubble sort

    Well, your C program was probably wrong and you were lucky. You can get away in C (not certain about its derivatives) with doing things wrong. As I recall, if you have back to back arrays declared in a C program.

    E.g. // C array declaration
    int a[10];
    int b[10];

    Then if you accessed a[10] you would be getting b[0]. Assuming your type boundaries were correct you would not get a bus error or segmentation violation. This is because C does not do boundary checking on array access.

    Java is much more strict in that aspect.

    Regards,
    Jim
    Last edited by jim829; 04-13-2014 at 01:38 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jdubya is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default Re: Bubble sort

    Yes. Initializing pass to 1 solved the problem. It continued to work in my C++ code too. Interesting. Thanks guys!

Similar Threads

  1. Bubble Sort
    By riyazm in forum New To Java
    Replies: 9
    Last Post: 03-04-2014, 03:49 PM
  2. Bubble Sort
    By Cruger in forum New To Java
    Replies: 2
    Last Post: 10-10-2013, 05:23 PM
  3. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 01:45 AM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06: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
  •