当前位置:网站首页>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!!!, 进行下次判断
}
}
}
}
边栏推荐
- Similarities and differences of QWidget, qdialog and qmainwindow
- Pangolin库链接库问题
- 『知识集锦』一文搞懂mysql索引!!(建议收藏)
- Alluxio为Presto赋能跨云的自助服务能力
- [image processing] image skeleton extraction based on central axis transformation with matlab code
- 微信怎么知道别人删除了你?批量检测方法(建群)
- 593. 有效的正方形 : 简单几何运用题
- 基于flask实现的mall商城---用户模块
- 如何使用“COPY –link”加速 Docker 构建和优化缓存
- LED透明屏和LED玻璃显示屏区别
猜你喜欢

Lucky draw system with background source code
![[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

【年中总结】创业3年,越来越穷,还是坚持架构平台

报表查询字段集sql摘记

ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching

LeetCode_容斥原理_中等_223.矩形面积

基于flask写的一个小商城mall项目

IPV6基础

如何在匹配行之前使用 grep 显示文件名和行号

2022年企业直播行业发展洞察
随机推荐
Alibaba architects spent a year sorting out the "Lucene advanced document", and you are also a big factory employee!
How to start writing helm charts for your kubernetes application
幸运抽奖系统带后台源码
Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
大伟 Golang之路
PaddleLite 编译以及代码跑通复盘
如何使用 grep 跨多行查找模式匹配
使用Tenserboard可视化深度学习训练过程
The interviewer training courseware (very practical in-house training courseware)
WPF 实现平移控件
LeetCode_容斥原理_中等_223.矩形面积
593. 有效的正方形 : 简单几何运用题
即学即用的问题解决思维,给无意识的生活装上“后视镜”
从零开始Blazor Server(3)--添加cookie授权
QML(二):设置自定义窗体
std::vector 拷贝、追加、嵌套访问
【年中总结】创业3年,越来越穷,还是坚持架构平台
共建共享数字世界的根:阿里云打造全面的云原生开源生态
SkiaSharp 之 WPF 自绘 弹动小球(案例版)
Learning with Recoverable Forgetting readings