1 public class BubbleSort implements IArraySort{ 2 3 4 @Override 5 public int[] sort(int[] arr) { 6 // 复制数组,不对入参修改 7 int[] newArr = Arrays.copyOf(arr, arr.length); 8 9 for (int i = 1; i < newArr.length; i++) { 10 // 是否完成排序标志位 11 boolean sortFlag = true; 12 13 for (int j = 0; j < newArr.length - i; j++) { 14 if (newArr[j] > newArr[j+1]){ 15 int temp = newArr[j]; 16 newArr[j] = newArr[j+1]; 17 newArr[j+1] = temp; 18 19 sortFlag = false; 20 } 21 } 22 23 if (sortFlag){ 24 break; 25 } 26 } 27 28 return newArr; 29 } 30 }
原文:https://www.cnblogs.com/huangwenhao1024/p/14451015.html