@ -0,0 +1,50 @@
|
||||
package chapter_sorting;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class bubble_sort {
|
||||
/* 冒泡排序 */
|
||||
static void bubbleSort(int[] nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 冒泡排序(标志优化)*/
|
||||
static void bubbleSortWithFlag(int[] nums) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
boolean flag = false; // 初始化标志位
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
flag = true; // 记录交换元素
|
||||
}
|
||||
}
|
||||
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSort(nums);
|
||||
System.out.println("排序后数组 nums = " + Arrays.toString(nums));
|
||||
|
||||
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSortWithFlag(nums1);
|
||||
System.out.println("排序后数组 nums1 = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
Before Width: | Height: | Size: 503 KiB |
After Width: | Height: | Size: 127 KiB |
Before Width: | Height: | Size: 626 KiB After Width: | Height: | Size: 172 KiB |
Before Width: | Height: | Size: 276 KiB |
After Width: | Height: | Size: 217 KiB |
Before Width: | Height: | Size: 490 KiB After Width: | Height: | Size: 318 KiB |