当前位置:网站首页>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;
}
边栏推荐
- Deep learning -yolov5 introduction to actual combat click data set training
- 【论文阅读】NFlowJS:基于鲁棒学习的合成负数据密集异常检测
- 进程和线程
- [email protected] raspberry pie
- wib3.0 跨越,在跨越(ง •̀_•́)ง
- Redis消息队列
- Self built DNS server, the client opens the web page slowly, the solution
- Station B, Master Liu Er - back propagation
- Embedded interview questions (IV. common algorithms)
- LTE CSFB process
猜你喜欢
PDK process library installation -csmc
continue和break的区别与用法
How to download GB files from Google cloud hard disk
What impact will frequent job hopping have on your career?
What is independent IP and how about independent IP host?
Li Chuang EDA learning notes 12: common PCB board layout constraint principles
初识数据库
A master in the field of software architecture -- Reading Notes of the beauty of Architecture
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
Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028
随机推荐
Self built DNS server, the client opens the web page slowly, the solution
[JVM] [Chapter 17] [garbage collector]
【课程笔记】编译原理
局域网同一个网段通信过程
Database: ODBC remote access SQL Server2008 in oracel
Deep learning -yolov5 introduction to actual combat click data set training
Embedded interview questions (I: process and thread)
(column 22) typical column questions of C language: delete the specified letters in the string.
AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
[email protected]树莓派
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
Embedded interview questions (IV. common algorithms)
Station B Liu Erden softmx classifier and MNIST implementation -structure 9
华为路由器如何配置静态路由
[cloud native] 3.1 kubernetes platform installation kubespher
How to use PHP string query function
28io stream, byte output stream writes multiple bytes
【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
实践分享:如何安全快速地从 Centos迁移到openEuler
Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production