Java 实现冒泡排序算法:
public class Bubble {
public static void bubble(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = { 2, 1, 3, 7, 6, 4, 5, 9, 8, 0 };
Bubble.bubble(arr, arr.length);
for (int i : arr) {
System.out.print(i + "\t");
}
}
}
本文介绍了一种使用Java实现的冒泡排序算法,并通过一个示例程序展示了如何对整数数组进行排序。该算法通过比较相邻元素并交换位置来完成排序过程。

245

被折叠的 条评论
为什么被折叠?



