冒泡:
public class BubbleSort{
public static int index = 0;
public static void main(String[] args) {
int arr[] = {23, 4, 21, 44, 25};
BubbleSort.printArr(arr);
int temp = 0;
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1; j++) {
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
BubbleSort.printArr(arr);
}
}
BubbleSort.printArr(arr);
}
public static void printArr(int arr[]){
System.out.println("------------the " + (index++) + " times-------------");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
} System.out.println();
}
}