Search code examples
javaruntime-errorbubble-sort

Run-time error when handling arrays and a bubble sort (Java)


I was recently assigned to create a bubble sort for a list of numbers (You'll find the list in the code I am about to show you), however, although the program compiles perfectly fine, I always get this run-time error in cmd...

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

Here is what I have so far.

//Ch 9 HW 7
class hw
{
    public static void main(String[] args)
    {
        int[] x = new int[10];
        x[0] = 100;
        x[1] = 23;
        x[2] = 15;
        x[3] = 23;
        x[4] = 7;
        x[5] = 23;
        x[6] = 2;
        x[7] = 311;
        x[8] = 5;
        x[9] = 8;
        x[10] = 3;
        System.out.println(bubbleSort(x));
    }
    public static int[] bubbleSort(int[] x)
    {
        int placehold = 0;
        for (int i = 0; i < x.length; i++)
        {
            if (x[i] > x[i + 1])
            {
                placehold = x[i + 1];
                x[i] = x[i + 1];
                x[i + 1] = placehold;
            }   
        }
        return x;
    }   
}

I was wondering if you guys could give me an explanation of the error and a way to fix it. Thanks a ton

~Andrew

EDIT: This code gives me a "cannot find symbol" error for "Arrays"

//Ch 9 HW 7
class hw
{
    public static void main(String[] args)
    {
        int[] x = {100, 23, 15, 23, 7, 23, 2, 311, 5, 8, 3};
        String y = Arrays.toString(bubbleSort(x));
        System.out.println(y);
    }
    public static int[] bubbleSort(int[] x)
    {
        int placehold = 0;
        for (int i = 0; i < x.length - 1; i++)
        {
            if (x[i] > x[i + 1])
            {
                placehold = x[i + 1];
                x[i] = x[i + 1];
                x[i + 1] = placehold;
            }   
        }
        return x;

    }   
}

FINAL EDIT W/ CORRECTIONS:

Here is the CORRECT code for those curious.

//Andrew Mancinelli
import java.util.*;
class hw
{ 
    public static void main(String[] args)
    {
        int[] x = {100, 23, 15, 23, 7, 23, 2, 311, 5, 8, 3};
        String y = Arrays.toString(bubbleSort(x));
        System.out.println(y);
    }
    public static int[] bubbleSort(int[] x)
    {
        for (int start = 0; start < x.length - 1; start++)
        {
            int min = x[start];
            int indexMin = start;
            for (int j = start + 1; j < x.length; j++)
            {
                if (x[j] < min)
                {
                    min = x[j];
                    indexMin = j;
                }   
            }
            x[indexMin] = x[start];
            x[start] = min;
        }
        return x;
    }   
}

Solution

  • Bug #1

    The array you created has capacity for 10 elements:

    int[] x = new int[10];
    

    If you look closely, you're assigning 11 elements:

    x[0] = 100;
    x[1] = 23;
    x[2] = 15;
    // ...
    x[10] = 3;
    

    So perhaps change the capacity to 11 when you declare it:

    int[] x = new int[11];
    

    But there's a much easier way to create that array:

    int[] x = {100, 23, 15, 23, 7, 23, 2, 311, 5, 8, 3};
    

    Notice that the size of the array is not specified here. As such, you cannot mistakenly give too small or too large size.

    Bug #2

    In this code, when i = x.length - 1, the expression x[i + 1] will be out of bounds:

    for (int i = 0; i < x.length; i++)
    {
        if (x[i] > x[i + 1])
        {
            placehold = x[i + 1];
            x[i] = x[i + 1];
            x[i + 1] = placehold;
        }   
    }
    

    To fix that, change the loop condition so that i doesn't reach x.length - 1:

    for (int i = 0; i < x.length - 1; i++)
    

    Bug 3

    Printing an int[] with System.out.println will not produce what you want. This is what you want:

    System.out.println(Arrays.toString(bubbleSort(x)));
    

    Without the Arrays.toString(...), the value [I@6d06d69c you saw is the hexadecimal representation of the hashcode of the array. This is the default implementation of Object.toString, which arrays inherit. It's pretty useless, the standard way to get a string representation of an array is using Arrays.toString(...).

    Note: Arrays is in java.util, so you need to add an import for this:

    import java.util.Arrays;