当前位置:网站首页>冒泡排序及优化 清楚直观 C语言
冒泡排序及优化 清楚直观 C语言
2022-06-10 00:06:00 【6映辰】
**
冒泡排序
**
一、冒泡排序概念
本质上就是比较两两相邻的关键字,如果前面的大就交换,否则就不变,直至需要排序的序列有序(默认从小到大)。 我在网上看到一个不错的动图,比较形象:
来源:https://blog.csdn.net/hcz666/article/details/117810787?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164722100016780271970540%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164722100016780271970540&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-2-117810787.pc_search_result_cache&utm_term=%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F&spm=1018.2226.3001.4187
二、具体思路
- 将带排序的记录放在数组arr[]中,对相邻的关键字依次进行比较,若为逆序则交换,否则不变
- 第二趟对前n-1个元素做同样的操作
- 重复以上比较和交换操作,直到在某一趟排序中,没有交换的操作,则完成排序
三、编写代码
**
1.冒泡排序
**
void BubbleSort(int *arr,int len)//冒泡排序
{
int i,j,flag;
for(i=0;i<len-1;i++)
{
flag=0;//将标志位置0
for(j=i+1;j<len;j++)
{
if(arr[i]>arr[j])
{
Swap(&arr[i],&arr[j]);//如果前一个数大,就交换位置
flag=1;//不是有序的,还要继续排序
}
}
if(!flag) return;//如果已经为有序的情况,结束排序
}
}
**
2.完整代码
**
#include <stdio.h>
void Swap(int *a,int *b)//交换
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void BubbleSort(int *arr,int len)//冒泡排序
{
int i,j,flag;
for(i=0;i<len-1;i++)
{
flag=0;//将标志位置0
for(j=i+1;j<len;j++)
{
if(arr[i]>arr[j])
{
Swap(&arr[i],&arr[j]);//如果前一个数大,就交换位置
flag=1;//不是有序的,还要继续排序
}
}
if(!flag) return;//如果已经为有序的情况,结束排序
}
}
int main()
{
int i;
int testArr[]={
10,9,1,4,13,6,7,9,21,0};
//进行排序
BubbleSort(testArr,sizeof(testArr)/sizeof(testArr[0]));
//输出打印
for(i=0;i<10;i++) printf("%d ",testArr[i]);
return 0;
}
四、运行结果
五、总结评价
冒泡排序算法特点:
- 稳定排序
- 可用于链式存储结构
- 时间复杂度高,为O(n^2),当初始无序,且n较大时,此算法不宜采用
有问题欢迎各位大佬指出
算法系列将持续更新,欢迎关注,一起学习
边栏推荐
- MySQL执行计划
- Record a malicious keyword brushed by illegal drainage
- Performance optimization scheme
- Do you know about singleton mode? I don't understand anyway.
- 929. unique email address
- Sarsa
- C # WPF background dynamic add control (Classic)
- Process test supports batch parameter import, and the test efficiency is directly full!
- 請問徽商期貨是正規的嗎?開戶交易安全嗎?
- Core [email protected] < 3.4 is no longer maintained and not recommended for usage due to the
猜你喜欢

On chip variation (OCV) concept learning

IDC authority predicts that China's manufacturing industry will soon take advantage of the cloud

Spingboot+quartrz cluster version realizes dynamic timing tasks (using reflection to realize custom services)

慢查询如何优化?(实战慢查询)

AI首席架构师5-AICA-文心NLP大模型技术及应用

Can I take the PMP Exam without project management experience?

谁说Redis不能存大key

Who says redis can't save big keys

wps怎么合并大小不一致的单元格

可重复读隔离级别的基石MVCC机制
随机推荐
Mind map - 3. SQL injection vulnerability
Pta7-6 quietly follows
请问徽商期货是正规的吗?开户交易安全吗?
(transfer) what is the difference between objectid, FID and oid fields in ArcGIS?
Cloudcompare & PCL principal curvature, mean curvature and Gaussian curvature calculation
[typecho]some problems in SQL programming
leetcode296场周赛
电力系统北斗时钟同步系统的组成和配置
478. 在圆内随机生成点
Idea uninstall tutorial
2022最新版阿里开发手册发布!!!
不用 if (obj != null) 判空
With the advent of the digital era, 360 has joined hands with the dark horse of entrepreneurship to help small and medium-sized enterprises seize the key future
478. randomly generate points in a circle
Operator (day 2)
The essence and soul of message queue
10 common high-frequency business scenarios that trigger IO bottlenecks
Introduction to C # WPF layout control layoutcontrol
Blue Bridge Cup_ Split cube_ Combinatorial mathematics_ Addition principle
约翰霍普金斯、加州伯克利等 | 利用序列和结构特征的机器学习实现抗体热稳定性的泛化预测