当前位置:网站首页>Bubble sort
Bubble sort
2022-06-26 05:50:00 【Wang Xiaoya】
Concept
Bubble sort (Bubble Sort), It's a kind of The field of computer science Simpler Sorting algorithm .
it Repeatedly Visited the element column to be sorted , Compare the two in turn adjacent The elements of , If the order ( : from large to small 、 First letter from Z To A) Mistakes are exchanged . The job of visiting an element is to do it repeatedly until no adjacent elements need to be exchanged , That is to say, the element column has been sorted .
The name of this algorithm comes from the fact that the smaller the elements, the more slowly “ floating ” Go to the top of the list ( Ascending or descending order ), Just as the bubbles of carbon dioxide in carbonated drinks eventually rise to the top , So the name “ Bubble sort ”.
Algorithm principle
The principle of bubbling is as follows :
Compare adjacent elements . If the first one is bigger than the second one , Just swap them .
Do the same for each pair of adjacent elements , From the beginning of the first couple to the end of the last couple . At this point , The last element should be the largest number .
Repeat the above steps for all elements , Except for the last one .
Keep repeating the above steps for fewer and fewer elements each time , Until there's no pair of numbers to compare .
flow chart :
Time complexity
If the initial state of the file is in positive order , One scan to complete sorting . Comparison of required keywords And record the number of moves All reach the minimum value , therefore , The best time for bubble sorting is O(n).
The worst time complexity of bubble sorting is O(n²).
Sum up , The total average time complexity of bubble sorting by O(n²)
Algorithm stability
Bubble sort is to move small elements forward or to move large elements back . Comparison is the comparison of two adjacent elements , The exchange also happens between these two elements . therefore , If two elements are equal , No more exchange ; If two equal elements are not adjacent , So even if we exchange the two in front of each other to make the two adjacent , It won't be exchanged at this time , So the order of the same elements hasn't changed , So bubble sort is a sort of Stable sequencing Algorithm .
Optimize
Aiming at problems :
After the order of the data is arranged , The bubbling algorithm will continue the next round of comparison , until arr.length-1 Time , The latter comparison is meaningless .
programme :
Set flag bit flag, If there is an exchange flag Set to true; If there is no exchange, set it to false.
So when a round of comparison is over, if flag Still false, namely : There was no exchange in this round , The data has been sequenced , There's no need to go on .
Example :
public static void BubbleSort1(int [] arr){
int temp;// Temporary variable
boolean flag;// Whether to exchange the logo
for(int i=0; i<arr.length-1; i++){ // Indicates the number of trips , altogether arr.length-1 Time
// Each traversal flag bit must be set to false, To determine whether the elements behind the exchange
flag = false;
for(int j=arr.length-1; j>i; j--){ // Select the maximum value of this sort and move back
if(arr[j] < arr[j-1]){
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
flag = true; // As long as there is an exchange ,flag Set as true
}
}
// Judge whether the flag bit is false, If false, It indicates that the following elements have been ordered , Just directly return
if(!flag) break;
}
}
Algorithm comparison :
Sorting algorithm | Average time complexity |
---|---|
Bubble sort | O(n²) |
Selection sort | O(n²) |
Insertion sort | O(n²) |
Shell Sort | O(n1.5) |
Quick sort | O(N*logN) |
Merge sort | O(N*logN) |
Heap sort | O(N*logN) |
Radix sorting | O(d(n+r)) |
边栏推荐
- BOM文档
- 家庭记账程序(第一版)
- Machine learning 07: Interpretation of PCA and its sklearn source code
- A new explanation of tcp/ip five layer protocol model
- Source code of findcontrol
- 适配器模式
- Some doubts about ARP deception experiment
- June 3 is a happy day
- Posting - don't get lost in the ocean of Technology
- Daily production training report (15)
猜你喜欢
Leetcode513. Find the value in the lower left corner of the tree
DOM document
pytorch(环境、tensorboard、transforms、torchvision、dataloader)
Pytorch (network model training)
REUSE_ ALV_ GRID_ Display event implementation (data_changed)
Redis usage and memory optimization
Consul service registration and discovery
421- binary tree (226. reversed binary tree, 101. symmetric binary tree, 104. maximum depth of binary tree, 222. number of nodes of complete binary tree)
Gram matrix
How to use the tablet as the second extended screen of the PC
随机推荐
定位设置水平,垂直居中(多种方法)
Consul服务注册与发现
Introduction to lcm32f037 series of MCU chip for motor
pytorch(网络模型训练)
The use of loops in SQL syntax
How to use the tablet as the second extended screen of the PC
Pytorch (network model)
Redis usage and memory optimization
Source code of findcontrol
[arm] add desktop application for buildreoot of rk3568 development board
类和对象的学习
There are applications related to web network request API in MATLAB (under update)
cross entropy loss = log softmax + nll loss
Learn cache lines and pseudo sharing of JVM slowly
ZigBee learning in simple terms Lecture 1
工厂方法模式、抽象工厂模式
Sql查询时间段内容
操作符的优先级、结合性、是否控制求值顺序【详解】
Redis discovery bloom filter
SDN based DDoS attack mitigation