冒泡排序代码:
public class BubbleSortText {
public static void main(String[] args) {
int[] arr = {5,1,2,3,4};
for (int i = 0; i < arr.length-1; i++) {
boolean flag = true;
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j]>arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = false;
}
}
if (flag) {
break;
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-1282.html