1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
public static int[] bubbleSort(int[] array) { int[] arr = Arrays.copyOf(array, array.length); int temp; int lastExchangeIndex = 0; int sortBorder = arr.length - 1; for (int i = 0; i < arr.length; i++) { boolean isSorted = true; for (int j = 0; j < sortBorder; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; isSorted = false; lastExchangeIndex = j; } } sortBorder = lastExchangeIndex; if (isSorted) { break; } } return arr; }
|
近期评论