当前位置:网站首页>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
边栏推荐
- 1223. 掷骰子模拟 范围DP
- S4/hana mm & SD EDI Nast based integrated configuration (orders, ordrsp, desadv, invoice)
- 智牛股--09
- Good news under the epidemic
- ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
- Macro task, micro task and event cycle mechanism
- Promise resolve callback hell, async await modifier
- 红娘的话
- [wechat applet] page navigation
- 面试重点——传输层的TCP协议
猜你喜欢

Leetcode 0919. complete binary tree inserter: array representation of complete binary tree

意向不到的Dubug妙招

initializer_list工具库学习

Idea sets get and set templates to solve the naming problem of boolean type fields

Duplicate numbers in array

Learning exploration-3d rotation card

Taobao Search case

Grain Academy p98 trample pit e.globalexceptionhandler: null

Optimize the browsing experience of yandere/konachan site with user scripts

Write a select drop-down list
随机推荐
Topsis与熵权法
面试重点——传输层的TCP协议
numeric学习之iota,accumulate
Vscode shortcut key: collapse and expand code
What is a physical firewall? What's the effect?
意向不到的Dubug妙招
Macro task, micro task and event cycle mechanism
Canada EE channel
[QNX hypervisor 2.2 user manual]9.7 generate
Which securities company should a novice choose to open an account? Is it safe?
[QNX Hypervisor 2.2用户手册]9.7 generate
1913. 两个数对之间的最大乘积差-无需排序法
[wechat applet] page navigation
Leetcode 0919. complete binary tree inserter: array representation of complete binary tree
762. 二进制表示中质数个计算置位
Practical skills of easyexcel
Summary of kotlin common knowledge points
[nodejs] nodejs create a simple server
《数据密集型应用系统设计》 - 应用系统概览
@Import

