当前位置:网站首页>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;
}
边栏推荐
- 查詢生產訂單中某個(些)工作中心對應的標准文本碼
- Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028
- OSPF configuration command of Huawei equipment
- Auto. JS learning notes 17: basic listening events and UI simple click event operations
- Easy to understand IIC protocol explanation
- B站刘二大人-线性回归及梯度下降
- Analysis of grammar elements in turtle Library
- Redis消息队列
- 嵌入式面试题(四、常见算法)
- Problems encountered in installing mysql8 on MAC
猜你喜欢

进程和线程

嵌入式面试题(四、常见算法)
![[happy Spring Festival] if you feel happy, dance](/img/b5/faa4cb94ef5fb45b8bb98ecb69962f.jpg)
[happy Spring Festival] if you feel happy, dance
[email protected] raspberry pie"/>[email protected] raspberry pie
![[Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS](/img/ed/8d112054f31bd7e593050d1278b9f1.jpg)
[Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS

H3C防火墙RBM+VRRP 组网配置

Application Security Series 37: log injection

华为路由器如何配置静态路由

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

First knowledge database
随机推荐
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
OSPF configuration command of Huawei equipment
Li Chuang EDA learning notes 12: common PCB board layout constraint principles
Clear floating mode
华为路由器忘记密码怎么恢复
Construction of yolox based on paste framework
[C language syntax] the difference between typedef struct and struct
Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
Easy to understand IIC protocol explanation
Rustdesk builds its own remote desktop relay server
华为BFD的配置规范
Hongliao Technology: how to quickly improve Tiktok store
Winter 2021 pat class B problem solution (C language)
Application Security Series 37: log injection
Zoom through the mouse wheel
26file filter anonymous inner class and lambda optimization
PDK工藝庫安裝-CSMC
What is independent IP and how about independent IP host?
CoDeSys note 2: set coil and reset coil
Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production
