public void bubblingSort(int[] nums){
// 判断是否为正经数组
if(nums == null || nums.length == 0);
// 冒泡排序
// count用于记录交换次数,这里设置为1,防止不进入循环,此后如果交换次数为0则表示已排好序无需在进行下去
int count = 1;
for(int i = 0; i < nums.length && count > 0; i++){
count = 0;
for(int j = nums.length - 1; j > i; j--){ // 从队尾开始冒泡
if(nums[j] < nums[j - 1]){ // 前一个数字大于后一个数字需要交换位置
int temp = nums[j];
nums[j] = nums[j - 1];
nums[j - 1] = temp;
count++;
}
}
}
}
时间复杂度:O(n^2)
空间复杂度:O(1)
力扣912题提交结果(只通过10个用例,时间复杂度过高)