当前位置:网站首页>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;
}
边栏推荐
- Web Security (VI) the use of session and the difference between session and cookie
- 27io stream, byte output stream, OutputStream writes data to file
- PDK工藝庫安裝-CSMC
- Web服务连接器:Servlet
- Jvxetable implant j-popup with slot
- Quantitative description of ANC noise reduction
- High quality coding tool clion
- 清除浮动的方式
- [happy Spring Festival] if you feel happy, dance
- Redis message queue
猜你喜欢
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
26file filter anonymous inner class and lambda optimization
59. Spiral matrix
Text classification still stays at Bert? The dual contrast learning framework is too strong
PDK process library installation -csmc
RustDesk 搭建一个自己的远程桌面中继服务器
High quality coding tool clion
Redis message queue
How to use PHP string query function
c语言——冒泡排序
随机推荐
AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
First knowledge database
Memory and stack related concepts
continue和break的区别与用法
Processes and threads
RustDesk 搭建一个自己的远程桌面中继服务器
Luogu [Beginner Level 4] array p1427 number game of small fish
Redis消息队列
B站刘二大人-数据集及数据加载 Lecture 8
29io stream, byte output stream continue write line feed
华为BFD的配置规范
High quality coding tool clion
B站刘二大人-Softmx分类器及MNIST实现-Lecture 9
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
CoDeSys note 2: set coil and reset coil
进程和线程
Database: ODBC remote access SQL Server2008 in oracel
Game push image / table /cv/nlp, multi-threaded start
Zoom through the mouse wheel
Text classification still stays at Bert? The dual contrast learning framework is too strong