【Java】冒泡排序
作者:
董懂同学 | 来源:发表于
2020-03-27 14:17 被阅读0次public class BubbleSort {
public static void bubbleSort(int[] array) {
int length = array.length;
for (int i = 0; i < length - 1; i++) { // 这个循环记录扫描到了第几轮
for (int j = 0; j < length - 1 - i; j++) { // 这个循环扫描交换未排序的数
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] nums = {30, 20, 40, 10};
bubbleSort(nums);
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
}
}
本文标题:【Java】冒泡排序
本文链接:https://www.haomeiwen.com/subject/tjpluhtx.html
网友评论