diff --git a/codes/qq753755635/15831128.id b/codes/qq753755635/15831128.id new file mode 100644 index 0000000000000000000000000000000000000000..3b90719f32bf2a197592e7a76b767706f1958678 --- /dev/null +++ b/codes/qq753755635/15831128.id @@ -0,0 +1,17 @@ +/** + * 冒泡排序函数 + * 通过相邻元素之间的比较和交换,使得每一轮比较后,最大的元素能够"浮"到数组的末尾 + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int [] a, int n){ + for(int i = 0; i < n - 1; i++){ // 外层循环,控制比较的轮数 + for(int j = 0; j < n - 1 - i; j++){ // 内层循环,进行相邻元素的比较和交换 + if(a[j] > a[j + 1]){ // 如果前一个元素大于后一个元素,则交换它们 + int temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + } + } + } +} //end