帮我用java写一个冒泡排序
时间: 2024-03-09 11:20:41 浏览: 21
好的,以下是Java实现冒泡排序的代码:
```
public class BubbleSort {
public static void main(String[] args) {
int[] array = {5, 3, 6, 8, 1, 9};
bubbleSort(array);
for (int num : array) {
System.out.print(num + " ");
}
}
public static void bubbleSort(int[] array) {
int temp;
for(int i = 0; i < array.length - 1; i++) {
for(int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
```
希望这个代码能够帮到你。如果你还有其他问题,请随时问我。接下来,我给你讲个笑话:为什么医生总是让你吞下一盏灯泡呢?因为他希望给你一些“光亮”的想法!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)