当前位置:网站首页>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
边栏推荐
- What is the difference between hot deployment and hot loading?
- 1913. 两个数对之间的最大乘积差-无需排序法
- Loading process such as reflection
- chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
- 【MUDUO】EventLoop事件循环
- Learning exploration - waves
- 【MUDUO】打包EventLoop和Thread
- JS regular expression content:
- Learning exploration-3d rotation card
- Regular expression (user name form verification / verification of landline number / regular replacement)
猜你喜欢

谷粒学苑P98踩坑 e.GlobalExceptionHandler : null

R语言安装教程 | 图文介绍超详细

C# - readonly 和 const 关键字

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

SAP Message No. VG202 IDoc E1EDK18 中付款条款已经转移:检查数据

Npm+ module loading mechanism

Write a select drop-down list

Docker 安装 Redis-5.0.12(远程访问)

S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)

S4/hana mm & SD EDI Nast based integrated configuration (orders, ordrsp, desadv, invoice)
随机推荐
意向不到的Dubug妙招
[QNX Hypervisor 2.2用户手册]9.7 generate
ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
Static agent + dynamic agent
Idea sets get and set templates to solve the naming problem of boolean type fields
typescript ts 基础知识之类
Taobao flexible.js file realizes flexible layout
Mongodb update operator (modifier)
Docker 安装 Redis-5.0.12(远程访问)
Strategy mode_
Why are there many snapshot tables in the BI system?
Canada EE channel
数组中重复的数字
[wechat applet] page navigation
@Import
TS basic data type
Npm+ module loading mechanism
[QNX Hypervisor 2.2用户手册]9.8 load
Serialize common default values and column parameters
死信队列 和消息TTL过期代码

