当前位置:网站首页>Let you understand bubble sorting (C language)
Let you understand bubble sorting (C language)
2022-06-11 11:55:00 【Butayarou】
【 introduce 】
We all know , In general , Bubbles will automatically float up slowly from under the water . So we can treat the data to be sorted as a bubble , And then, according to the conditions we control “ Bubble ” People “ Automatically ” Float to the corresponding position . On the whole , The data became orderly .
【 Manual demonstration 】
Let's start at the worst ( A set of data is completely in reverse order ) Let's start with the analysis ( Assume that the data are int type ):
We can find out , The number of times is the number of elements minus 1(len-1). The number of comparisons per trip varies with the ordinal number of trips , Apparent success Function relation , And their sum is equal to the number of elements Mathematical relations .
【 Code implementation 】
Then convert the above draft to the following code :
void BubbleSort (int arr[], int len) // The ginseng : The array to sort and the number of array elements
{
int i=0;
for (i=0; i<len-1; i++) // The outer loop controls the number of sortations
{
int j=0;
for (j=0; j<len-1-i; j++) // The inner loop controls the comparison times of each sort
{
if (arr[j] > arr[j+1]) // The exchange condition of two data ( To make data in ascending or descending order )
{
int tmp = arr[j]; // With the help of temporary variables, the two data are exchanged
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
You can see , The integer processed in the previous trip , It has been lined up and put in the back , Therefore, there is no need to visit again in the next trip .
Test other use cases , It can also get the expected results .
What if there are multiple elements with equal values ?
If you actually implement the above code , Two elements with equal values will be close to each other during sorting , Once they are adjacent , Then subsequent sorting operations will not change their relative positions , So the bubble sort algorithm is More stable Of .
Time complexity :
Actually , Not just int Data of type , It can also be other types of data :
void BubbleSort (elemType arr[], int len)
{
int i=0;
for (i=0; i<len-1; i++)
{
int j=0;
for (j=0; j<len-1-i; j++)
{
if (arr[j] > arr[j+1])
{
elemType tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
【 Use examples 】
For example, the elements of the structure array are sorted according to the average score of the students :
void BubbleSort (struct data stu[], int len)
{
int i=0;
for (i = 0; i < len-1; i++)
{
int j=0;
for (j = 0; j < len-1-i; j++)
{
if (stu[j].average < stu[j + 1].average) // Sort from large to small
{
struct data tmp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = tmp;
}
}
}
}
Or according to the student's name ( character string ) To sort the elements of the structure array : The above if Change statement to
if (strcmp(stu[j].name, stu[j + 1].name) > 0) that will do .
More articles :
Let you understand the sorting of choices (C Language )
边栏推荐
- 爱可可AI前沿推介(6.11)
- WordPress landing page beautification plug-in: recommended by login Designer
- PS does not display text cursor, text box, and does not highlight after selection
- Etcd介绍
- File excel export
- Uncaught TypeError: Cannot set property ‘next‘ of undefined 报错解决
- 2019 book list
- WordPress user name modification plug-in: username changer
- Publish WordPress database cache plug-in: DB cache reloaded 3.1
- JS interview questions - arrow function, find and filter some and every
猜你喜欢

安全工程师发现PS主机重大漏洞 用光盘能在系统中执行任意代码

Qt中radioButton使用

CVPR 2022 | 文本引导的实体级别图像操作ManiTrans

Gestion de projets logiciels 7.1. Concept de base du calendrier du projet

The tutor transferred me 800 yuan and asked me to simulate a circuit (power supply design)

中级web开发工程师,面试题+笔记+项目实战

导师转我800块,让我仿真一个电路(电源设计)

C# 设置或验证 PDF中的文本域格式

C# 将OFD转为PDF

广东市政安全施工资料管理软件2022新表格来啦
随机推荐
Maximum water container
深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)
Streaking? Baa!
解决swagger文档接口404的问题
推荐几款Gravatar头像缓存插件
吊打面试官,涨姿势
Zhejiang University and Microsoft Asia Research Institute released a new method of video recognition, which can recognize video frame by frame without data marking, or can be used for sign language tr
软件项目管理 7.1.项目进度基本概念
WordPress数据库缓存插件:DB Cache Reloaded
Runtime reconfiguration of etcd
[JUC supplementary] atomic class, unsafe
iframe 传值
Memory mapping image of the grayloc module in the surfacefinder process
JS to realize the rotation chart (riding light). Pictures can be switched left and right. Moving the mouse will stop the rotation
C # convert ofd to PDF
WordPress landing page beautification plug-in: recommended by login Designer
Queuing theory model
Read geo expression matrix
Interview experience of Xiaomi Android development post~
Mongodb usage