当前位置:网站首页>C language bubble sort
C language bubble sort
2022-07-06 05:51:00 【bit..】
Bubble sort (Bubble Sort) It is also a simple and intuitive sorting algorithm . It repeatedly visits the sequence to be sorted , Compare two elements at a time , If they're in the wrong order, exchange them . The job of the interview sequence is to repeat until there is no need to exchange , That is to say, the sequence 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 .
As one of the simplest sorting algorithms , Bubble sorting makes me feel like Abandon It feels the same in a word book , Every time on the first page , So most familiar with . Bubble sorting also has an optimization algorithm , It's about making a flag, When elements are not exchanged in a sequence traversal , It is proved that the sequence has been ordered . But this improvement doesn't do much to improve performance .
1. Algorithm steps
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 . After this step , The last element will be the maximum 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 .
2. Dynamic diagram demonstration
3. When is the fastest
When the input data is already in positive order .
4. When is the slowest
When the input data is in reverse order ( Write a for It's OK to output data in reverse order )
5. Code implementation ( From big to small )
#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]) // If you want to arrange from small to large, just put '<' Change to '>' That is to say
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[5], i;
printf(" Please enter five numbers :");
for (i = 0; i < 5; i++)
{
scanf_s("%d", &arr[i]);
}
printf(" Array before arrangement :");
for (i = 0; i < 5; i++)
{
printf("%d", arr[i]);
}
printf("\n");
printf(" Sorted array :");
Bubble_sort(arr, 5);
for (i = 0; i < 5; i++)
{
printf("%d", arr[i]);
}
return 0;
}
边栏推荐
- B站刘二大人-数据集及数据加载 Lecture 8
- PDK process library installation -csmc
- Zoom through the mouse wheel
- 05. Security of blog project
- 初识数据库
- Rustdesk builds its own remote desktop relay server
- Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028
- [C language syntax] the difference between typedef struct and struct
- 01. Project introduction of blog development project
- After the project is released, index Html is cached
猜你喜欢
B站刘二大人-数据集及数据加载 Lecture 8
ArcGIS应用基础4 专题图的制作
How to use PHP string query function
P2802 go home
Market development prospect and investment risk assessment report of China's humidity sensor industry from 2022 to 2028
Station B, Master Liu Er - back propagation
Redis消息队列
29io stream, byte output stream continue write line feed
授予渔,从0开始搭建一个自己想要的网页
The usage and difference between strlen and sizeof
随机推荐
Redis消息队列
AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
LeetCode_ String inversion_ Simple_ 557. Reverse word III in string
My 2021
Pytorch代码注意的细节,容易敲错的地方
B站刘二大人-反向传播
【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028
Jushan database appears again in the gold fair to jointly build a new era of digital economy
CoDeSys note 2: set coil and reset coil
C language learning notes (mind map)
How to use PHP string query function
Web Security (V) what is a session? Why do I need a session?
【经验】UltralSO制作启动盘时报错:磁盘/映像容量太小
How can large websites choose better virtual machine service providers?
巨杉数据库再次亮相金交会,共建数字经济新时代
Station B Liu Erden - linear regression and gradient descent
RustDesk 搭建一个自己的远程桌面中继服务器
Clear floating mode
嵌入式面试题(一:进程与线程)