当前位置:网站首页>c语言——冒泡排序
c语言——冒泡排序
2022-07-06 05:44:00 【bit..】
冒泡排序(Bubble Sort)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢"浮"到数列的顶端。
作为最简单的排序算法之一,冒泡排序给我的感觉就像 Abandon 在单词书里出现的感觉一样,每次都在第一页第一位,所以最熟悉。冒泡排序还有一种优化算法,就是立一个 flag,当在一趟序列遍历中元素没有发生交换,则证明该序列已经有序。但这种改进对于提升性能来说并没有什么太大作用。
1. 算法步骤
比较相邻的元素。如果第一个比第二个大,就交换他们两个。
对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。
针对所有的元素重复以上的步骤,除了最后一个。
持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
2. 动图演示

3. 什么时候最快
当输入的数据已经是正序时。
4. 什么时候最慢
当输入的数据是反序时(写一个 for 循环反序输出数据不就行了)
5. 代码实现(由大到小)
#include<stdio.h>
void Bubble_sort(int arr[], int size)
{
int i, j, tmp;
for (i = 0; i <= size - 1; i++)
{
for (j = 0; j <= size - 1 - i; j++)
{
if (arr[j] < arr[j + 1]) //如果要由小到大排列只需将'<'改成'>'即可实现
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[5], i;
printf("请输入五个数:");
for (i = 0; i < 5; i++)
{
scanf_s("%d", &arr[i]);
}
printf("排列前的数组:");
for (i = 0; i < 5; i++)
{
printf("%d", arr[i]);
}
printf("\n");
printf("排序后的数组:");
Bubble_sort(arr, 5);
for (i = 0; i < 5; i++)
{
printf("%d", arr[i]);
}
return 0;
}
边栏推荐
- H3C V7版本交换机配置IRF
- 实践分享:如何安全快速地从 Centos迁移到openEuler
- Download, install and use NVM of node, and related use of node and NRM
- Installation de la Bibliothèque de processus PDK - csmc
- 网站进行服务器迁移前应做好哪些准备?
- 【经验】win11上安装visio
- C Advanced - data storage (Part 1)
- 05. 博客项目之安全
- [force buckle]43 String multiplication
- 【torch】|torch. nn. utils. clip_ grad_ norm_
猜你喜欢

Vulhub vulnerability recurrence 72_ uWSGI

大型网站如何选择比较好的云主机服务商?

Winter 2021 pat class B problem solution (C language)

Analysis of grammar elements in turtle Library

05. 博客项目之安全

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

自建DNS服务器,客户端打开网页慢,解决办法
![[force buckle]43 String multiplication](/img/fd/de63e6185af4b6293e748aaf7cee29.jpg)
[force buckle]43 String multiplication

H3C V7版本交换机配置IRF

Summary of deep learning tuning tricks
随机推荐
巨杉数据库再次亮相金交会,共建数字经济新时代
Web Security (V) what is a session? Why do I need a session?
After the project is released, index Html is cached
华为BFD的配置规范
Easy to understand IIC protocol explanation
Vulhub vulnerability recurrence 73_ Webmin
[SQL Server fast track] - authentication and establishment and management of user accounts
Download, install and use NVM of node, and related use of node and NRM
How can large websites choose better virtual machine service providers?
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
[email protected] raspberry pie
Station B Liu Erden - linear regression and gradient descent
Rustdesk builds its own remote desktop relay server
【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
28io stream, byte output stream writes multiple bytes
Station B, Master Liu Er - back propagation
Clear floating mode
[machine learning notes] univariate linear regression principle, formula and code implementation
01. Project introduction of blog development project
C Advanced - data storage (Part 1)
