用JAVA写一个冒泡排序
时间: 2023-05-18 08:05:13 浏览: 67
好的,我给你来个简单的示例:
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 3, 2, 4, 1};
int temp;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
阅读全文