冒泡排序源代码
package sort;
public class BubleSort {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int[] a = bubbleSort(arr);
for (int b : a) {
System.out.println(b);
}
}
public static int[] bubbleSort(int arr[]) {
if (arr.length <= 1) {
return arr;
}
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
int temp = 0;
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
}
网友评论