public class Solution {
// 冒泡排序
public void bubbleSort(int[] nums) {
if (nums == null || nums.length <= 1) {
return;
}
int n = nums.length;
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - 1 - i; ++j) {
if (nums[j] > nums[j + 1]) {
swap(nums, j, j + 1);
}
}
}
}
// 交换函数
private void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
网友评论