请使用JAVA冒泡排序算法编写程序,实现对数组{25,24,12,76,101,96,28}的排序:
时间: 2024-09-20 21:08:33 浏览: 50
当然可以,下面是一个简单的Java冒泡排序算法的例子,用于对给定的数组进行排序:
```java
public class BubbleSortExample {
public static void main(String[] args) {
int[] array = {25, 24, 12, 76, 101, 96, 28};
bubbleSort(array);
System.out.println("Sorted array is:");
for (int num : array) {
System.out.print(num + " ");
}
}
// 冒泡排序函数
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 交换元素
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
```
这个程序首先定义了一个`bubbleSort`函数,它使用两层嵌套循环来比较并按顺序排列数组中的元素。然后,在`main`函数中,我们创建了一个数组并调用了`bubbleSort`对其进行排序,最后打印出排序后的结果。
阅读全文
相关推荐

















