当前位置:网站首页>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!!!, 进行下次判断
}
}
}
}
边栏推荐
- The interviewer training courseware (very practical in-house training courseware)
- [image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
- mysql单行,多行子查询
- Insights into the development of the enterprise live broadcast industry in 2022
- 2022 latest WiFi master applet independent version 3.0.8
- Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
- 企业微信客户朋友圈一天可以发多少条?都有哪些限制?如何突破朋友圈可展示人数限制?
- 如何使用“COPY –link”加速 Docker 构建和优化缓存
- 就这?TypeScript其实并不难!(建议收藏)
- "100 Interview Knowledge Collections" 1. Interview Skills丨Do you really understand HR's careful thinking?
猜你喜欢
暑假集训week1
Learning with Recoverable Forgetting readings
[image processing] image skeleton extraction based on central axis transformation with matlab code
Peking University open classes are coming! Welcome to the "AI for science" class
Alluxio为Presto赋能跨云的自助服务能力
Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life
three.js 报错信息 RGBELoader.js:46 RGBELoader Bad File Format: bad initial token
Function comparison between report control FastReport and stimulus soft
The interviewer training courseware (very practical in-house training courseware)
考完PMP后有什么益处
随机推荐
Flink UDF 函数汇总
IPv6 Foundation
即学即用的问题解决思维,给无意识的生活装上“后视镜”
Use anyio instead of asyncio
Hutool日期时间
DNS protocol, ICMP protocol, NAT technology
Proficient in audio and video development can really do whatever you want
593. 有效的正方形 : 简单几何运用题
MarkDown高阶语法手册
Qt 之自定义界面(实现无边框、可移动)
2022 latest WiFi master applet independent version 3.0.8
从零开始Blazor Server(3)--添加cookie授权
TCP和UDP
GBase8s核心数据备份
面试官培训课件(非常实用的企业内训课件)
Pangolin库链接库问题
From scratch Blazor Server (3) - add cookie authorization
【无标题】
sql join中on条件后接and和where
Learning with Recoverable Forgetting readings