冒泡排序编译但超出范围

时间:2014-12-07 13:04:17

标签: java sorting bounds

我遇到了让数组的简单冒泡排序正常工作的问题。 它编译,但运行程序时我得到越界异常

我知道出界的错误是什么,但我不明白为什么会出现这种情况。

那里有哪些天才知道如何解决这个问题?谢谢

  public class BubbleSort
{

    public static void main(String[] args)
    {
        // create the array that we want to sort with buble sort alogorithm
        int intArray[] = new int[]{5, 90, 35, 45, 150, 3};

        //print the array before the bubble sort
        System.out.println("Array before Bubble Sort");
        for (int i = 0; i < intArray.length; i++)
        {
            System.out.print(intArray[i] + " ");
        }

        // sort an array using bubble sort algorithm
        bubbleSort(intArray);
        System.out.println("");

        // print array after sorting using bubble sort
        System.out.println("Array after Bubble Sort");

        for (int i = 0; i < intArray.length; i++)
        {
            System.out.print(intArray[i] + "");
        }

    }

    private static void bubbleSort(int[] intArray)
    {
        int n = intArray.length;
        int hold = 0;

        for (int i = 0; i < n; i++) //allows us to pass or loop around array
        {
            for (int j = 1; j < (n - i); j++) //allows on pass or comparison
            {
                if (intArray[j] > intArray[j + 1]) //swap the elements!
                {
                    hold = intArray[j];
                    intArray[j] = intArray[j + 1];
                    intArray[j + 1] = hold;
                }
            }
        }
    }
}

5 个答案:

答案 0 :(得分:1)

在外循环的第一次迭代中,i==0

因此,内圈j会从1转到n-i-1==n-1

j==(n-1)时,intArray[j+1]会抛出ArrayIndexOutOfBoundsException

答案 1 :(得分:1)

bubbleSort方法的外循环的第一次迭代中,当i等于零时,j最多可以升至n-i-1,包括即j的最后一个值为n-1

发生这种情况时,inArray[j+1]将超出范围,因为它等同于inArray[n]。最后一个有效索引是n-1

要解决此问题,请确保外部循环从i=1开始,而不是i=0

答案 2 :(得分:0)

 

 public class BubbleSort
 {
  public static void main (String[]args)
  {
    // create the array that we want to sort with buble sort alogorithm
    int intArray[] = new int[] {5, 90, 35, 45, 150, 3};

    //print the array before the bubble sort
    System.out.println ("Array before Bubble Sort");
      for (int i = 0 ; i  intArray[j+1])  //swap the elements!
        {

        hold = intArray[j];
        intArray[j] = intArray[j + 1];
        intArray[j + 1] = hold;
   }
  }
 }

}

}

替换j&lt; (n-i),j < (N-1)-1。
的原因
在最后一次迭代中,j将具有等于比数组长度小1的值(即,它指向最后一个元素)。但是当你比较索引j + 1的元素时,你指向一个超出范围的元素。因此例外。希望它有所帮助。

答案 3 :(得分:0)

我评论了你犯了错误的行

public static class BubbleSort { //you need static here

    public static void main(String[] args) {
        int intArray[] = new int[] { 5, 90, 35, 45, 150, 3 };
        System.out.println("Array before Bubble Sort");
        for (int i = 0; i < intArray.length; i++) {
            System.out.print(intArray[i] + " ");
        }
        bubbleSort(intArray);
        System.out.println("");
        System.out.println("Array after Bubble Sort");
        for (int i = 0; i < intArray.length; i++) {
            System.out.print(intArray[i] + " "); // you need space between the parenthesis so that results show up readable
        }
    }

    private static void bubbleSort(int[] intArray) {
        int n = intArray.length;
        int hold = 0;

        for (int i = 0; i < n; i++){
            for (int j = 0; j < (n - i - 1); j++){ // j has to be equal to 0, or your first value in the array won't get compared, and -1 because of the array out of bounds error
                if (intArray[j] > intArray[j + 1]){
                    hold = intArray[j];
                    intArray[j] = intArray[j + 1];
                    intArray[j + 1] = hold;
                }
            }
        }
    }
}

答案 4 :(得分:0)

(这是为了回答你关于3不是显示的第一个值的问题) 如果您想要同意订单,代码应该是这样的 你错过了(int j = 0; j&lt;(n-i-1); j ++

for (int i=0; i<n; i++) //loop trough array
        {
           for( int j=0; j<(n-i-1); j++) //loop allows variable j pass on for comparison
           {
               if ( intArray[j]> intArray[j+1]) // if current slot in array j is bigger than the next one