当前位置:网站首页>冒泡排序【C语言】
冒泡排序【C语言】
2022-07-06 09:16:00 【薇远镖局】
冒泡排序是最简单的排序方法,理解起来容易。虽然它的计算步骤比较多,不是最快的,但它是最基本的,初学者一定要掌握。
冒泡排序的原理是:从左到右,相邻元素进行比较。每次比较一轮,就会找到序列中最大的一个或最小的一个。这个数就会从序列的最右边冒出来。
以从小到大排序为例,第一轮比较后,所有数中最大的那个数就会浮到最右边;第二轮比较后,所有数中第二大的那个数就会浮到倒数第二个位置……就这样一轮一轮地比较,最后实现从小到大排序。
比如对下面这个序列进行从小到大排序:
90 21 132 -58 34第一轮:
1、90 和 21比,90>21,则它们互换位置:
21 90 132 -58 342、90 和 132 比,90<132,则不用交换位置。
3、132 和 –58 比,132>–58,则它们互换位置:
21 90 -58 132 344、132 和 34 比,132>34,则它们互换位置:
21 90 -58 34 132到此第一轮就比较完了。第一轮的结果是找到了序列中最大的那个数,并浮到了最右边。
比较时,每轮中第 n 次比较是新序列中第 n 个元素和第 n+1 个元素的比较(假如 n 从 1 开始)。
第二轮:
1、21 和 90 比,21<90,则不用交换位置。
2、90 和 –58 比,90>–58,则它们互换位置:
21 -58 90 34 1323、90 和 34 比,90>34,则它们互换位置:
21 -58 34 90 132到此第二轮就比较完了。第二轮的结果是找到了序列中第二大的那个数,并浮到了最右边第二个位置。
第三轮:
1、21 和 –58 比,21>–58,则它们互换位置:
-58 21 34 90 1322) 21 和 34 比,21<34,则不用交换位置。
到此第三轮就比较完了。第三轮的结果是找到了序列中第三大的那个数,并浮到了最右边第三个位置。
第四轮:
1、–58 和 21 比,–58<21,则不用交换位置。
至此,整个序列排序完毕。从小到大的序列就是“–58 21 34 90 132”。从这个例子中还可以总结出,如果有 n 个数据,那么只需要比较 n–1 轮。而且除了第一轮之外,每轮都不用全部比较。因为经过前面轮次的比较,已经比较过的轮次已经找到该轮次中最大的数并浮到右边了,所以右边的数不用比较也知道是大的。
下面写一个程序:
# include <stdio.h>
int main(void)
{
int a[] = {900, 2, 3, -58, 34, 76, 32, 43, 56, -70, 35, -234, 532, 543, 2500};
int n; //存放数组a中元素的个数
int i; //比较的轮数
int j; //每轮比较的次数
int buf; //交换数据时用于存放中间数据
n = sizeof(a) / sizeof(a[0]); /*a[0]是int型, 占4字节, 所以总的字节数除以4等于元素的个数*/
for (i=0; i<n-1; ++i) //比较n-1轮
{
for (j=0; j<n-1-i; ++j) //每轮比较n-1-i次,
{
if (a[j] < a[j+1])
{
buf = a[j];
a[j] = a[j+1];
a[j+1] = buf;
}
}
}
for (i=0; i<n; ++i)
{
printf("%d\x20", a[i]);
}
printf("\n");
return 0;
}输出结果是:
2500 900 543 532 76 56 43 35 34 32 3 2 -58 -70 -234程序中,为什么每轮比较的次数是 j<n–1–i,而不是 j<n–1?
因为冒泡排序有一个特点,这个程序是从大到小排序,所以第一轮排序以后,最小的数就会浮到最右面;第二轮排序以后,第二小的数会浮到倒数第二个位置;第三轮排序以后,第三小的数会浮到倒数第三个位置……也就是说,排序多少轮,就有多少个数字已经按排序要求排好了,它们不需要再比较。写 j<n–1 也可以,只不过程序在执行时多做了许多无用功。
边栏推荐
- Solution to the practice set of ladder race LV1 (all)
- FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
- Pytorch实现简单线性回归Demo
- [Flink] Flink learning
- 数据库面试常问的一些概念
- FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
- Integration test practice (1) theoretical basis
- 2020 WANGDING cup_ Rosefinch formation_ Web_ nmap
- 【yarn】Yarn container 日志清理
- MongoDB
猜你喜欢

PHP - whether the setting error displays -php xxx When PHP executes, there is no code exception prompt

Face recognition_ recognition

Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)
![[yarn] CDP cluster yarn configuration capacity scheduler batch allocation](/img/85/0121478f8fc427d1200c5f060d5255.png)
[yarn] CDP cluster yarn configuration capacity scheduler batch allocation

{一周总结}带你走进js知识的海洋

Basic use of pytest

MySQL realizes read-write separation

Apprentissage automatique - - régression linéaire (sklearn)

STM32型号与Contex m对应关系

FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
随机推荐
人脸识别 face_recognition
Yarn installation and use
机器学习--决策树(sklearn)
vs2019 使用向导生成一个MFC应用程序
电商数据分析--用户行为分析
FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
MATLAB学习和实战 随手记
ES6 promise object
Contiki源码+原理+功能+编程+移植+驱动+网络(转)
Nodejs connect mysql
【Flink】CDH/CDP Flink on Yarn 日志配置
MySQL realizes read-write separation
4、安装部署Spark(Spark on Yarn模式)
MySQL与c语言连接(vs2019版)
About string immutability
【presto】presto 参数配置优化
When using lambda to pass parameters in a loop, the parameters are always the same value
[Bluebridge cup 2020 preliminary] horizontal segmentation
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
分布式節點免密登錄