Search code examples
javaindexoutofboundsexceptionbubble-sort

Array index out of bounds


Although I tried to solve the exception using break it still fails on the input "321". Code for bubble sort on hackerrank.

The error occurs on if(a[i+1]==n).

import java.io.*;``
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] a = new int[n];
    for(int a_i=0; a_i < n; a_i++){
        a[a_i] = in.nextInt();
    }
    // Write Your Code Here
    int numSwaps=0;
    for(int i=0;i<n;i++){
        if(a[i+1]==n){       // error occurs here
            break;
        }
        else{
                if(a[i]>a[i+1]){
                int temp=a[i];
                a[i]=a[i+1];
                a[i+1]=temp;
                numSwaps++;
                }
            }
    }
    //firstElement=a[0];
    //lastElement=a[n-1];
    System.out.println("Array is sorted in"+" "+numSwaps+" "+"swaps."+"\n"+"First Element:"+" "+a[0]+"\n"+"Last Element:"+" "+a[n-1]);
}

}


Solution

  • Your condition i<n will overflow when i=n-1, because you are adding i+1, you are referencing the array out-of-bounds.

    The fix is easy, however, change the condition to i<n-1.

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int[] a = new int[n];
            for(int a_i=0; a_i < n; a_i++){
                a[a_i] = in.nextInt();
            }
            // Write Your Code Here
            int numSwaps=0;
            for(int i=0;i<n-1;i++){
                if(a[i+1]==n){
                    break;
                } else if(a[i]>a[i+1]){
                    int temp=a[i];
                    a[i]=a[i+1];
                    a[i+1]=temp;
                    numSwaps++;
                }
            }
            //firstElement=a[0];
            //lastElement=a[n-1];
            System.out.println("Array is sorted in"+" "+numSwaps+" "+"swaps."+"\n"+"First Element:"+" "+a[0]+"\n"+"Last Element:"+" "+a[n-1]);
    }
    

    Also, take bit of pride in code style; I did a couple touch-ups, but its far from clean.