-2

When the below method is run it throws an ArrayIndexOutOfBoundsException at line 179, which is the if statement in the nested loop.

I was thinking it had something to do with the index of j-1. So I took the -1 out and just had j in the brackets, but it threw the same exception at the first line of the swap. I've looked for bubble sort syntax and from what I can tell is I'm good. I'm close, I know. Any suggestions to fix this?

public static void bubbleSort(int[]array1){
    int temp = 0;

    for(int i = 0; i < array1.length; i++){
        for(int j = 0; j < (array1.length - i); j++){
            if(array1[j-1] > array1[j]){
                //swap
                temp = array1[j-1];
                array1[j-1] = array1[j];
                array1[j] = temp;
            }
        }
    }
}
JNYRanger
  • 6,829
  • 12
  • 53
  • 81

4 Answers4

0

The only problem because of which you are getting this error is the case when j = 0, because at that time j-1 = -1. Which is not a valid index value in java and hence arrayIndexOutOfBounds exception. I have made the necessary change in the code. Have a look in the snippet below.

public static void bubbleSort(int[]array1){

            int temp = 0;
                for(int i = 0; i < array1.length; i++){
                    for(int j = 0; j < (array1.length - i-1); j++){
                        if(array1[j] > array1[j+1]){
                            //sawp
                            temp = array1[j];
                            array1[j] = array1[j+1];
                            array1[j+1] = temp;
Chandan Purbia
  • 285
  • 4
  • 14
0

The first element of the array is 0. With j-1 you get -1 and this is not in the range of the array.

You tried to put just "j". But, I think that you put "j+1" to compare. And the arrayIndexOutOfBounds exception was throwed because of the "j+1".

Try to use this at the second loop:

(array1.length - i - 1)
LucasDelboni
  • 119
  • 1
  • 4
0

You are getting this exception because you have initiated j=0 and doing j-1. In that case it is going out of bound and hence giving exception. Code below will work fine.

public static void bubbleSort(int[] array1){

            int temp = 0;
                for(int i = 0; i < array1.length; i++){
                    for(int j = 1; j < array1.length-i; j++){
                        if(array1[j-1] > array1[j]){
                            //sawp
                            temp = array1[j-1];
                            array1[j-1] = array1[j];
                            array1[j] = temp;
                        }
               }
           }
Amit Aggrawal
  • 59
  • 1
  • 10
0

For first iteration of inner loop when j=0, j-1 becomes -1 and arr[-1] is index out of bound exception. Now when you change j-1 to j in if condition

if(array1[j-1] > array1[j]) // j-1 to j

Still in the first line swap arr[j-1] is arr[-1] which throws index out of bound exception.

Please follow the below code to avoid index out of bound exception. Start j's iteration from 1.

`public static void bubbleSort(int[]array1){
    int temp = 0;
    for(int i = 0; i < array1.length; i++){
        for(int j = 1; j < (array1.length - i); j++){ //initialize j=1 instead 0
            if(array1[j-1] > array1[j]){
                //sawp
                temp = array1[j-1];
                array1[j-1] = array1[j];
                array1[j] = temp;
            }
        }
    }
}`
Sanjay Barnwal
  • 203
  • 1
  • 4