当前位置:网站首页>冒泡排序的简单理解
冒泡排序的简单理解
2022-07-03 06:04:00 【xuhss_com】
优质资源分享
| 学习路线指引(点击解锁) | 知识定位 | 人群定位 |
|---|---|---|
| 🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
| Python量化交易实战 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
详细描述
冒泡排序是一种交换排序,基本思想是在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。
即每当两个相邻的数比较后发现它们的顺序与排序要求相反时,就将它们互换。
冒泡排序详细的执行步骤如下:
- 从第一个元素开始,比较相邻的元素,如果前一个比后一个大,就交换它们两个;
- 从前往后,对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素就会是最大的数;
- 将这次找出的最大元素放在最后一个元素位置上,然后针对除这个最大元素以外的其他所有元素重复以上 1~2 步骤;
- 重复以上步骤,直到最后第一个元素和第二个元素完成比较、交换,则排序完成。
算法图解

问题解疑
冒泡排序是原地排序算法吗?
冒泡排序是一个原地排序算法,过程只涉及相邻数据的交换操作,只需要常量级的临时空间,它的空间复杂度是 O(1)。
冒泡排序是稳定的排序算法吗?
为了保证冒泡排序算法的稳定性,当有相邻的两个元素大小相等时可以不做交换,相同大小的数据在排序前后不会改变顺序,所以冒泡排序是稳定的排序算法。
冒泡排序的时间复杂度是多少?
使用最优时间复杂度解法,原序列是有序时的时间复杂度是 O(n);最坏情况时间复杂度为 O(n2);平均时间复杂度是 O(n2)。
代码实现
| | package cn.fatedeity.sort; |
| | |
| | public class BubbleSort { |
| | private static void swap(int[] numbers, int src, int target) { |
| | int temp = numbers[src]; |
| | numbers[src] = numbers[target]; |
| | numbers[target] = temp; |
| | } |
| | |
| | public static int[] sort(int[] numbers) { |
| | for (int i = 0; i < numbers.length - 1; i++) { |
| | boolean doSwap = false; |
| | for (int j = 0; j + 1 < numbers.length - i; j++) { |
| | if (numbers[j] > numbers[j + 1]) { |
| | swap(numbers, j, j + 1); |
| | doSwap = true; |
| | } |
| | } |
| | // 优化基础冒泡排序的步骤 |
| | if (!doSwap) { |
| | // 如果遍历整个序列无需交换,则表示整个序列已经完全有序 |
| | return numbers; |
| | } |
| | } |
| | return numbers; |
| | } |
| | } |
边栏推荐
- [teacher Zhao Yuqiang] use the catalog database of Oracle
- Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
- Naive Bayes in machine learning
- 理解 期望(均值/估计值)和方差
- pytorch DataLoader实现miniBatch(未完成)
- Why should there be a firewall? This time xiaowai has something to say!!!
- The server data is all gone! Thinking caused by a RAID5 crash
- Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
- pytorch 搭建神经网络最简版
- About the difference between count (1), count (*), and count (column name)
猜你喜欢

Project summary --2 (basic use of jsup)

深入解析kubernetes controller-runtime

pytorch 搭建神经网络最简版

Multithreading and high concurrency (7) -- from reentrantlock to AQS source code (20000 words, one understanding AQS)
![[teacher Zhao Yuqiang] calculate aggregation using MapReduce in mongodb](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] calculate aggregation using MapReduce in mongodb

Kubesphere - Multi tenant management

Advanced technology management - do you know the whole picture of growth?

Es remote cluster configuration and cross cluster search

Use abp Zero builds a third-party login module (I): Principles

【系统设计】邻近服务
随机推荐
Luogu problem list: [mathematics 1] basic mathematics problems
[teacher Zhao Yuqiang] RDB persistence of redis
Kubernetes notes (IV) kubernetes network
Es remote cluster configuration and cross cluster search
Kubesphere - Multi tenant management
[teacher Zhao Yuqiang] index in mongodb (Part 2)
Oauth2.0 - using JWT to replace token and JWT content enhancement
Convolution operation in convolution neural network CNN
Why should there be a firewall? This time xiaowai has something to say!!!
Jackson: what if there is a lack of property- Jackson: What happens if a property is missing?
深度学习,从一维特性输入到多维特征输入引发的思考
[teacher Zhao Yuqiang] kubernetes' probe
Maximum likelihood estimation, divergence, cross entropy
Svn branch management
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
Kubesphere - set up redis cluster
Leetcode solution - 02 Add Two Numbers
[teacher Zhao Yuqiang] use the catalog database of Oracle
Exportation et importation de tables de bibliothèque avec binaires MySQL
从小数据量 MySQL 迁移数据到 TiDB