当前位置:网站首页>Simple understanding of bubble sorting
Simple understanding of bubble sorting
2022-07-03 06:29:00 【xuhss_ com】
High quality resource sharing
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system |
Detailed description
Bubble sort is a sort of exchange sort , The basic idea is to sort in a set of numbers , All numbers in the range that are not currently ordered , Compare and adjust the two adjacent numbers from top to bottom , Let the larger number sink , Smaller up .
That is, whenever two adjacent numbers are compared and their order is found to be opposite to the sorting requirements , Just swap them .
The detailed implementation steps of bubble sorting are as follows :
- Let's start with the first element , Compare adjacent elements , If the former is bigger than the latter , Just exchange the two of them ;
- Before and after , Do the same for each pair of adjacent elements , From the beginning of the first couple to the end of the last couple , So the last element will be the largest number ;
- Put the largest element found this time on the last element position , Then repeat the above for all elements except the largest element 1~2 step ;
- Repeat the above steps , Until the last element and the second element are compared 、 In exchange for , Then the sorting is finished .
Algorithm diagram

Problem solving
Is bubble sorting an in place sort algorithm ?
Bubble sort is an in place sort algorithm , The process only involves the exchange of adjacent data , Only temporary space at the constant level is needed , Its spatial complexity is O(1).
Is bubble sort a stable sort algorithm ?
In order to ensure the stability of bubble sorting algorithm , When there are two adjacent elements of equal size, no exchange is required , Data of the same size will not change the order before and after sorting , So bubble sort is a stable sort algorithm .
What is the time complexity of bubble sorting ?
Using the optimal time complexity solution , The time complexity when the original sequence is ordered is O(n); The worst case time complexity is O(n2); The average time complexity is O(n2).
Code implementation
| | 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; |
| | } |
| | } |
| | // Steps to optimize basic bubble sorting |
| | if (!doSwap) { |
| | // If you traverse the entire sequence, you don't need to exchange , It means that the whole sequence has been completely ordered |
| | return numbers; |
| | } |
| | } |
| | return numbers; |
| | } |
| | } |
边栏推荐
- Openresty best practices
- 论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》
- Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"
- Floating menu operation
- GPS坐标转百度地图坐标的方法
- How to scan when Canon c3120l is a network shared printer
- Exportation et importation de tables de bibliothèque avec binaires MySQL
- [set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
- Important knowledge points of redis
- Phpstudy setting items can be accessed by other computers on the LAN
猜你喜欢

Redis cluster creation, capacity expansion and capacity reduction

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

Support vector machine for machine learning

Oauth2.0 - Introduction and use and explanation of authorization code mode
![[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)](/img/a4/00aca72b268f77fe4fb24ac06289f5.jpg)
[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)

YOLOV2学习与总结

【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案

“我为开源打榜狂”第一周榜单公布,160位开发者上榜

SQL实现将多行记录合并成一行

Selenium - by changing the window size, the width, height and length of different models will be different
随机推荐
tabbar的设置
表达式的动态解析和计算,Flee用起来真香
堆排序和优先队列
Example of joint use of ros+pytoch (semantic segmentation)
Kubesphere - build Nacos cluster
100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
Common interview questions
【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
ssh链接远程服务器 及 远程图形化界面的本地显示
Kubesphere - set up redis cluster
.NET程序配置文件操作(ini,cfg,config)
ThreadLocal的简单理解
Jackson: what if there is a lack of property- Jackson: What happens if a property is missing?
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
scroll-view指定滚动元素的起始位置
Use @data in Lombok to simplify entity class code
Docker advanced learning (container data volume, MySQL installation, dockerfile)
Kubesphere - Multi tenant management
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
The win7 computer can't start. Turn the CPU fan and stop it