2014-12-07 86 views
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.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; 
       } 
      } 
     } 
    } 
} 

回答

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; 
       } 
      } 
     } 
    } 
} 
+0

非常好。你的灵魂固定它。谢谢您的帮助 – CassyHiggins 2014-12-07 13:30:15

1

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

因此在内部循环j将从1n-i-1==n-1

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

1

在您bubbleSort法的外循环的第一次迭代,当i等于零,j被允许上去n-i-1,包容性,即j的最后一个值将是n-1

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

要解决这个问题,请确保外部循环始于i=1而不是i=0

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; 
    } 
    } 
} 

} 

} 

替换Ĵ<(N-1)其中j <(N-1)-1。
原因
在上次迭代中,j的值等于比数组长度小1的值(即它指向最后一个元素)。但是,当您比较索引为j + 1的元素时,您指向的是一个出界元素。因此例外。希望能帮助到你。

+0

奇怪没有显示整个代码为什么 – T90 2014-12-07 13:15:11

+0

感谢了这一点解决了界失误综观在这里输出...为什么是5秒直到泡泡排序后名单上的第一个?不应该是3,5,35等(按升序排列)? – CassyHiggins 2014-12-07 13:24:45

0

线(这是回答你的问题约3不被显示的第一个值) 代码应该是这样的,如果你同意的顺序 希望你错过了(INT J = 0;Ĵ<(NI-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