当前位置:网站首页>2.1冒泡排序(Bubble Sorting)
2.1冒泡排序(Bubble Sorting)
2022-07-29 11:41:00 【TUJC】
2.1、冒泡排序
2.5.1、基本介绍
通过对待排序序列从前向后,依次比较相邻元素的值,若发现逆序则交换,使值较大的元素逐渐从前移向后部,就象水底下的气泡一样逐渐向上冒。

小结上面的图解过程:
(1)一共进行数组的大小-1次大的循环;
(2)每一趟排序的次数在逐渐的减少;
(3)如果我们发现在某趟排序中,没有发生一次交换,可以提前结束冒泡排序。这个就是优化
优化:
因为排序的过程中,各元素不断接近自己的位置,如果一趟比较下来没有进行过交换,就说明序列有序,因此要在排序过程中设置一个标志flag判断元素是否进行过交换。从而减少不必要的比较。(这里说的优化,可以在冒泡排序写好后,在进行)
2.1.2 代码实例
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class BubbleSort {
public static void main(String[] args) {
// int arr[] = {3, 9, -1, 10, 20};
//
// System.out.println("排序前");
// System.out.println(Arrays.toString(arr));
//为了容量理解,我们把冒泡排序的演变过程,给大家展示
//测试一下冒泡排序的速度O(n^2), 给80000个数据,测试
//创建要给80000个的随机的数组
int[] arr = new int[80000];
for(int i =0; i < 80000;i++) {
arr[i] = (int)(Math.random() * 8000000); //生成一个[0, 8000000) 数
}
Date data1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1Str = simpleDateFormat.format(data1);
System.out.println("排序前的时间是=" + date1Str);
//测试冒泡排序
bubbleSort(arr);
Date data2 = new Date();
String date2Str = simpleDateFormat.format(data2);
System.out.println("排序后的时间是=" + date2Str);
//System.out.println("排序后");
//System.out.println(Arrays.toString(arr));
/* // 第二趟排序,就是将第二大的数排在倒数第二位 for (int j = 0; j < arr.length - 1 - 1 ; j++) { // 如果前面的数比后面的数大,则交换 if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } System.out.println("第二趟排序后的数组"); System.out.println(Arrays.toString(arr)); // 第三趟排序,就是将第三大的数排在倒数第三位 for (int j = 0; j < arr.length - 1 - 2; j++) { // 如果前面的数比后面的数大,则交换 if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } System.out.println("第三趟排序后的数组"); System.out.println(Arrays.toString(arr)); // 第四趟排序,就是将第4大的数排在倒数第4位 for (int j = 0; j < arr.length - 1 - 3; j++) { // 如果前面的数比后面的数大,则交换 if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } System.out.println("第四趟排序后的数组"); System.out.println(Arrays.toString(arr)); */
}
// 将前面额冒泡排序算法,封装成一个方法
public static void bubbleSort(int[] arr) {
// 冒泡排序 的时间复杂度 O(n^2), 自己写出
int temp = 0; // 临时变量
boolean flag = false; // 标识变量,表示是否进行过交换
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
// 如果前面的数比后面的数大,则交换
if (arr[j] > arr[j + 1]) {
flag = true;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
//System.out.println("第" + (i + 1) + "趟排序后的数组");
//System.out.println(Arrays.toString(arr));
if (!flag) {
// 在一趟排序中,一次交换都没有发生过
break;
} else {
flag = false; // 重置flag!!!, 进行下次判断
}
}
}
}
边栏推荐
- MyCat中间件高可用、读写分离、分片、主从切换、ER分片
- HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
- 如何在匹配行之前使用 grep 显示文件名和行号
- Meituan and hungry were interviewed by Hangzhou supervisors to implement the responsibility of food safety management and prohibit malicious competition
- 为什么应该在开发环境中使用 Kubernetes
- 怎么以管理员身份运行cmd?以管理员身份运行cmd方法介绍
- Is this it?TypeScript actually not difficult!(recommended collection)
- It is recommended to collect a thousand ways to write sql row to column!!
- 1.MySQL数据库的介绍
- Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem
猜你喜欢
![[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code](/img/c1/f962f1c1d9f75732157d49a5d1d0d6.png)
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code

IPV6基础

Peking University open classes are coming! Welcome to the "AI for science" class

如何开始为您的 Kubernetes 应用程序编写 Helm 图表

QML(一):自定义圆角按钮的处理

多元宇宙:重塑新商业格局

AI model risk assessment Part 2: core content

Based on the flask to write a small shopping mall project

Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem

MyCat中间件高可用、读写分离、分片、主从切换、ER分片
随机推荐
谷歌“消灭” Cookie 计划延至 2024 年
『知识集锦』一文搞懂mysql索引!!(建议收藏)
golang 实现文件上传下载
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
黑马四小时入门学习记录-2|本地应用
AMH6.X升级到AMH7.0后,登录后台提示MySQL连接出错怎么解决?
Applied practical skills of deep reinforcement learning
文件上传漏洞
std::vector 拷贝、追加、嵌套访问
Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
暑假集训week1
Basic. Blocking
SkiaSharp of WPF custom painting to bounce ball (case)
大伟 GBase8s游标稳定性读ESQL测试用例
Learning with Recoverable Forgetting阅读心得
Pangolin库链接库问题
Flink UDF 函数汇总
Learning with Recoverable Forgetting readings
INVALID_ ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
力扣sql刷题(四)