当前位置:网站首页>C language - bubble sort
C language - bubble sort
2022-07-19 05:43:00 【numb and dying】
One 、 Description of the problem
Bubble sort (Bubble Sort), It's a simpler sort algorithm in the field of computer science .
It repeatedly visits the element columns to be sorted , Compare two adjacent elements in turn , If the order ( : from large to small 、 First letter from Z To A) Mistakes are exchanged . The job of visiting elements 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 like the bubbles of carbonated drinks will eventually rise to the top , So the name “ Bubble sort ”.
Two 、 Algorithm principle
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 .
3、 ... and 、 Algorithm implementation
#include <stdio.h>
void Bubble_Sort(int arr[], int n) {
int flag = 1;
for (int i = 0; i < n - 1; i++) {
flag = 1;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
flag = 0;
}
}
if (flag == 1) {
break;
}
}
}
int main() {
int arr[30] = {0};
int n = 0;
printf(" Please enter the size of the array :\n");
scanf("%d", &n);
printf(" Enter each element in turn :\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("\n Original array :");
for (int i = 0; i < n; i++) {
if (i == (n - 1)) {
printf("%d\n", arr[i]);
break;
}
printf("%d, ", arr[i]);
}
Bubble_Sort(arr, n);
printf("\n Array after sorting :");
for (int i = 0; i < n; i++) {
if (i == (n - 1)) {
printf("%d\n", arr[i]);
break;
}
printf("%d, ", arr[i]);
}
return 0;
}Four 、 Algorithm testing

5、 ... and 、 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 stable sort algorithm .
边栏推荐
猜你喜欢

微信小程序密码显示隐藏(小眼睛)

JNA加载DLL及在jar中的运用

基于四叉树的图像压缩问题

cuda11.0的pytorch安装小计

软件过程与管理复习(八)

【语音识别入门】基础概念与框架

10 question 10 answer: do you really know thread pools?

3.东软跨境电商数仓项目架构设计
![[first launch in the whole network] will an abnormal main thread cause the JVM to exit?](/img/ae/5df25d64c2f29292bbfb21f696bbb0.png)
[first launch in the whole network] will an abnormal main thread cause the JVM to exit?

MySQL learning notes (5) -- join join table query, self join query, paging and sorting, sub query and nested query
随机推荐
安卓实现真正安全的退出app
Application and principle of throttle/debounce
replace限制文本框只能输入数字,数字和字母等的正则表达式
MySQL queries the data of the current day, this week, this month and last month
JNI实用笔记
Custom components of wechat applet
VS 中 error C4996: ‘scanf‘: This function or variable may be unsafe. 的解决方法。
1.东软跨境电商数仓需求规格说明文档
MySQL事务
1.東軟跨境電商數倉需求規格說明文檔
1 sparksql overview
电商用户行为实时分析系统(Flink1.10.1)
cuda11.0的pytorch安装小计
正则替换group(n)内容
SnackBar source code analysis and packaging
C language & bit field
关于AS安装之后的一些细碎事项
widerperson数据集转化为YOLOv5训练格式,并加入到crowdhuman中
Unable to determine Electron version. Please specify an Electron version
1. Neusoft cross border e-commerce warehouse demand specification document