通过冒泡排序对数组进行排序[重复](Sorting of array by bubble sort [duplicate])

这个问题在这里已有答案:

导致java.lang.ArrayIndexOutOfBoundsException的原因是什么以及如何阻止它? 19个答案

我正在尝试使用Java中的冒泡排序算法对数组进行排序。 但是当我运行代码时会发生ArrayIndexOutofBoundException 。 这是我的代码

package bubblesort; public class BubbleSort { public int[] sort(int [] arr){ int temp=0; for(int i=0 ; i<arr.length ; i++) for(int j=0 ; j<arr.length-i ; j++){ if(arr[j] > arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; }} return arr; } public static void main(String[] args) { BubbleSort ob = new BubbleSort(); int[]nums={2,5,1,55}; System.out.println("Sorted list is:"); int[]sorted =ob.sort(nums); for(int i=0 ; i<nums.length;i++) System.out.println(nums[i]); } }

This question already has an answer here:

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? 24 answers

I am trying to sort the array using bubble sort algo in Java. But when I run the code an ArrayIndexOutofBoundException occurs. Here is my code

package bubblesort; public class BubbleSort { public int[] sort(int [] arr){ int temp=0; for(int i=0 ; i<arr.length ; i++) for(int j=0 ; j<arr.length-i ; j++){ if(arr[j] > arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; }} return arr; } public static void main(String[] args) { BubbleSort ob = new BubbleSort(); int[]nums={2,5,1,55}; System.out.println("Sorted list is:"); int[]sorted =ob.sort(nums); for(int i=0 ; i<nums.length;i++) System.out.println(nums[i]); } }

最满意答案

由于你的内部循环引用arr[j+1] ,它应该更快地终止一步,而不是迭代到最后一个元素:

for(int i = 0 ; i < arr.length; i++) for(int j = 0 ; j < arr.length - i - 1; j++) { // Here ------------------------^

Since your inner loop references arr[j+1], it should terminate one step sooner, and not iterate up to the last element:

for(int i = 0 ; i < arr.length; i++) for(int j = 0 ; j < arr.length - i - 1; j++) { // Here ------------------------^通过冒泡排序对数组进行排序[重复](Sorting of array by bubble sort [duplicate])

这个问题在这里已有答案:

导致java.lang.ArrayIndexOutOfBoundsException的原因是什么以及如何阻止它? 19个答案

我正在尝试使用Java中的冒泡排序算法对数组进行排序。 但是当我运行代码时会发生ArrayIndexOutofBoundException 。 这是我的代码

package bubblesort; public class BubbleSort { public int[] sort(int [] arr){ int temp=0; for(int i=0 ; i<arr.length ; i++) for(int j=0 ; j<arr.length-i ; j++){ if(arr[j] > arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; }} return arr; } public static void main(String[] args) { BubbleSort ob = new BubbleSort(); int[]nums={2,5,1,55}; System.out.println("Sorted list is:"); int[]sorted =ob.sort(nums); for(int i=0 ; i<nums.length;i++) System.out.println(nums[i]); } }

This question already has an answer here:

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? 24 answers

I am trying to sort the array using bubble sort algo in Java. But when I run the code an ArrayIndexOutofBoundException occurs. Here is my code

package bubblesort; public class BubbleSort { public int[] sort(int [] arr){ int temp=0; for(int i=0 ; i<arr.length ; i++) for(int j=0 ; j<arr.length-i ; j++){ if(arr[j] > arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; }} return arr; } public static void main(String[] args) { BubbleSort ob = new BubbleSort(); int[]nums={2,5,1,55}; System.out.println("Sorted list is:"); int[]sorted =ob.sort(nums); for(int i=0 ; i<nums.length;i++) System.out.println(nums[i]); } }

最满意答案

由于你的内部循环引用arr[j+1] ,它应该更快地终止一步,而不是迭代到最后一个元素:

for(int i = 0 ; i < arr.length; i++) for(int j = 0 ; j < arr.length - i - 1; j++) { // Here ------------------------^

Since your inner loop references arr[j+1], it should terminate one step sooner, and not iterate up to the last element:

for(int i = 0 ; i < arr.length; i++) for(int j = 0 ; j < arr.length - i - 1; j++) { // Here ------------------------^