当前位置:网站首页>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;
}
边栏推荐
- [imgui] unity MenuItem shortcut key
- [machine learning notes] univariate linear regression principle, formula and code implementation
- B站刘二大人-数据集及数据加载 Lecture 8
- Anti shake and throttling are easy to understand
- Station B Liu Erden - linear regression and gradient descent
- How to get list length
- Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028
- LeetCode_字符串反转_简单_557. 反转字符串中的单词 III
- Practice sharing: how to safely and quickly migrate from CentOS to openeuler
- Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
猜你喜欢

Web服务连接器:Servlet

【经验】win11上安装visio

Practice sharing: how to safely and quickly migrate from CentOS to openeuler

MIT6.s081-2020 Lab2 System Calls

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

Li Chuang EDA learning notes 12: common PCB board layout constraint principles

Problems encountered in installing mysql8 on MAC

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

网站进行服务器迁移前应做好哪些准备?

数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
随机推荐
PDK工藝庫安裝-CSMC
Market development prospect and investment risk assessment report of China's humidity sensor industry from 2022 to 2028
PDK process library installation -csmc
wib3.0 跨越,在跨越(ง •̀_•́)ง
Download, install and use NVM of node, and related use of node and NRM
How to download GB files from Google cloud hard disk
C language learning notes (mind map)
[happy Spring Festival] if you feel happy, dance
26file filter anonymous inner class and lambda optimization
The usage and difference between strlen and sizeof
28io stream, byte output stream writes multiple bytes
Selective parameters in MATLAB functions
H3C V7版本交换机配置IRF
[string] palindrome string of codeup
Hongliao Technology: how to quickly improve Tiktok store
[Jiudu OJ 07] folding basket
Analysis of grammar elements in turtle Library
Summary of data sets in intrusion detection field
Migrate Infones to stm32
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
