当前位置:网站首页>Bubble sort idea and Implementation
Bubble sort idea and Implementation
2022-07-25 23:44:00 【Bin * Zai】
Bubble sort idea and implementation
1、 What is bubble sorting
The core idea of bubble sorting : Compare two adjacent elements
A set of data 【9 8 7 6 5 4 3 2 1 0】, Arrange them as 【0 1 2 3 4 5 6 7 8 9】.
A bubble sort brings a data to where it should eventually appear .
- 10 Data needs to be 9 Bubble sort ,n Data needs to be n-1 Bubble sort .
- 10 When performing the first bubble sort of data , Compare the data in pairs , Need to compare 9 Time . The second time , Need to compare 8 Time .n Data , The first bubble sort needs to be compared n-1 Time , The second trip needs to compare n-2 Time ……
2、 Arrange the data by bubble sorting
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> void bubble_sort(int arr[], int sz) { int i = 0; for (i = 0; i < sz - 1; i++) { int flag = 1; // Suppose the array is ordered int j = 0; for (j = 0; j < sz - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; flag = 0; } } if (flag == 1) { break; // It shows that it is really orderly , Stop the cycle , Stop sorting , Save time } } } int main() { int arr[] = { 9,8,7,6,5,4,3,2,1,0 }; int i = 0; int sz = sizeof(arr) / sizeof(arr[0]); bubble_sort(arr, sz); for (i = 0; i < sz; i++) { printf("%d ", arr[i]); } return 0; }Print the results
边栏推荐
- Serialize operator
- 【代码案例】博客页面设计(附完整源码)
- Generating random number random learning uniform_ int_ distribution,uniform_ real_ distribution
- Canada EE channel
- [QNX hypervisor 2.2 user manual]9.8 load
- Es5 new method
- [test technology performance test LoadRunner] detailed explanation of common functions of LoadRunner
- R language installation tutorial | graphic introduction is super detailed
- 统计之歌 歌词
- 数组中重复的数字
猜你喜欢

Query commodity cases (operate data with array addition method) / key points

Node Foundation

疫情之下的好消息

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

How does JS judge whether the current date is within a certain range

学习探索-波浪
![[JUC] concurrent keyword volatile](/img/80/2f1b33f1e8c87fd4f8806eafb83139.png)
[JUC] concurrent keyword volatile

Why are there many snapshot tables in the BI system?

S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)

Npm+ module loading mechanism
随机推荐
762. Prime number calculation setting in binary representation
Read the field status of account in ABAP code (hidden, optional, required)
Native JS perfectly realizes deep copy
762. 二进制表示中质数个计算置位
[test technology performance test LoadRunner] detailed explanation of common functions of LoadRunner
Scroll case: return to top with animation
2022 Niuke multi School Game 2
TS class
Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data
Array merge method: concat()
【代码案例】博客页面设计(附完整源码)
Static agent + dynamic agent
From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
Loading process such as reflection
initializer_list工具库学习
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
The late Apple co-founder Steve Jobs was posthumously awarded the U.S. presidential medal of freedom
热部署和热加载有什么区别?
S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)
[wechat applet] page navigation

