package bubbleSort; import java.util.Arrays; public class TestBubbleSort { public static void main(String[] args) { int[] arr = new int[] { 9, 8, 7, 6, 5, 4, 2, 1 }; System.out.println("The unsorted array is : " + Arrays.toString(arr)); System.out.println("The sorted array with bubble sort algorithm is : " + Arrays.toString(BubbleSortImpl.bubbleSort(arr))); } } public class BubbleSortImpl { //bubble sort public static int[] bubbleSort(int[] arr) { int len = arr.length; int temp = Integer.MIN_VALUE; for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - 1 - i; j++) { if (arr[j + 1] < arr[j]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; } } /*运行结果: The unsorted array is : [9, 8, 7, 6, 5, 4, 2, 1] The sorted array with bubble sort algorithm is : [1, 2, 4, 5, 6, 7, 8, 9] */