当前位置:网站首页>C language classic exercises (2) - "bubble sort"“
C language classic exercises (2) - "bubble sort"“
2022-07-24 03:28:00 【it_ NunU】
C Interesting language exercises —— Bubble sort
List of articles
One 、 Introduction to bubble sort
Bubble sort (Bubble Sort), Also known as settlement sequencing , The reason why it is called bubble sorting is that the algorithm will put relatively small ( Big ) Data like bubbles in the water gradually rise to the top of the array . meanwhile , more ( Small ) The data of will also sink to the bottom of the array step by step , This process needs to be repeated many times in the whole array range . Every time you execute , Compare two adjacent elements , And determine whether to exchange positions .
Two 、 Principle of graphic interpretation

Pure manual drawing !
3、 ... and 、 Code implementation
1. Code example
// Bubble sort bubble sort
void Bubble_sort(int a[], int sz)
{
int i = 0, j = 0;
for ( i = 0; i < sz - 1; i++)// The outer loop sz-1 Time
{
for (j = 0; j < sz - 1- i; j++) // Inner circulation
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
// Print array functions
void print(int arr[],int sz)
{
int i = 0;
for ( i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int i = 0;
int arr[10] = {
2,1,8,6,9,7,3,5,4,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
printf(" Please input data :\n");
for ( i = 0; i < sz; i++)
{
scanf("%d", arr + i);
}
//print(arr, sz); // Print the array data before sorting
Bubble_sort(arr, sz);
print(arr, sz);
return 0;
}
2. Code parsing
Code parsing : For the in the array sz(sz Is the number of array elements ) Number of execution sz -1 Operate in sequence , In the outer loop of each , Perform the following operations on the unsorted elements in the array : Compare two adjacent elements , If the subscript is j The element of is greater than the subscript j + 1 The element is , Then exchange its position , Each judgment and exchange is counted as a bubble sorting completion , So after sz - 1 After several operations, you can arrange all the numbers from small to large .
explain : Inner circulation j < sz - i - 1: Because there are sz Elements j It's from 0 Start , For the first time 9 Only after several comparisons can the largest number be exchanged to the last , Then the array subscript is 0~ 8 Then with Subscript to be 0(+1)~8(+1) Than the size , So when arr[j] by sz - 1 - 1 == 8 When It's over , And so on, the second round sz - 2 - 1…
summary
Bubble sort in general , Easy to understand , The code implementation is simple and not complicated , But the only disadvantage is the low efficiency , Because in every exchange , An element can only be judged one by one and then exchanged , If in an array with large array elements , It is undoubtedly a shortcoming ! So can the partners come up with a better solution ? I hope to make progress with you !
If you feel helpful, please give me a compliment
边栏推荐
- Secondary development of ArcGIS JS API -- loading national sky map
- 轮播图van-swipe的报错:cannot read a properties of null(reading width)
- Ue5 split screen (small map) solution
- Basic syntax of MySQL DDL and DML and DQL
- Gpushare.com | 如何使用TensorBoardX可视化工具?
- 在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
- Appendtofile append failed
- IDEA Clone的项目报Cannot resolve symbol ‘Override‘
- 21st day of written test mandatory training
- What is the security of Treasury reverse repo
猜你喜欢

Xiaodi and Xiaohui

错误代码0x80004005

C自定义类型详解

JS Array isaarray () Type of

The error of van swipe in the rotation chart: cannot read a properties of null (reading width)

Data Lake: comparative analysis of open source data Lake schemes deltalake, Hudi and iceberg

Simulink code generation: variable subsystem and its code

FTP服务与配置

Microsoft win11/10 package manager Winget will support the installation of applications from zip files

Basic use of Pinia
随机推荐
FTP service and configuration
Cannot resolve symbol 'override' of idea clone‘
Network parameter management
Write code, and multiple characters move from both ends to converge in the middle
Babylon.js cool canvas background animation JS special effects
Data Lake: introduction to Apache Hudi
Android Development - lambda expression of kotlin syntax
Test interview questions
Basic syntax of MySQL DDL and DML and DQL
Embedded system transplantation [5] - Cross compilation tool chain
什么是IMU?
Redux Usage Summary
uva1445
C文件操作详解
STL multimap
A simple and perfect WPF management system framework source code
How will you answer the "Hello world" challenge written in C language?
C. Minimum Ties-Educational Codeforces Round 104 (Rated for Div. 2)
The error of van swipe in the rotation chart: cannot read a properties of null (reading width)
B. Eastern Exhibition- Codeforces Round #703 (Div. 2)