当前位置:网站首页>[implementation of bubble sorting]
[implementation of bubble sorting]
2022-06-11 02:36:00 【Cat star people who love Durian】
List of articles
1、 Bubble sort implementation
Tips : The time complexity of bubble sort is :F(N)=N*(N-1)/2
#include<stdio.h>
int main()
{
int arr[10] = {
1,6,4,3,9,5,0,7,2,8 };
int sz = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < sz - 1; i++)
{
int flag = 0;
for (int 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 = 1;
}
}
if (flag == 0)
break;
}
for (int i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Running results :
Add a picture that is convenient for drawing and understanding :
2、 Function implementation of bubble sort
#include<stdio.h>
void BubbleSort(int* a, int n)
{
for (int end = n; end > 1; --end)
{
int exchange = 0;
for (int i = 1; i < end; ++i)
{
if (a[i - 1] > a[i])
{
int tmp = a[i - 1];
a[i - 1] = a[i];
a[i] = tmp;
exchange = 1;
}
}
if (exchange == 0)
break;
}
}
int main()
{
int arr[10] = {
1,6,4,3,9,5,0,7,2,8 };
int sz = sizeof(arr) / sizeof(arr[0]);
BubbleSort(arr, sz);
for (int i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Running results :
The above is the whole content of this article , If there are mistakes in the article or something you don't understand , Communicate more with meow bloggers . Learn from each other and make progress . If this article helps you , Can give meow bloggers a concern , Your support is my biggest motivation .
边栏推荐
- Setting access to win10 shared folder without verification
- Jetpack compose box control
- When the interviewer opens his mouth, he comes to compose. Is this the case now?
- Jetpack Compose Scaffold和BottomAppBar(底部导航)
- Web watermark
- Learning Tai Chi Maker - esp8226 (II)
- GCC C内联汇编
- 力扣刷题篇——哈希表
- Mentality cannot collapse
- JS summary of math functions in math objects
猜你喜欢

当逻辑删除遇上唯一索引,遇到的问题和解决方案?

企业展厅设计能为企业带来什么?

clang-format 最全格式说明

2022 safety officer-a certificate special operation certificate examination question bank and simulation examination

Project load failed

【新晋开源项目】动态配置化任务编排框架 Gobrs-Async 加入Dromara开源社区

English subtitle video translated into Chinese subtitles

A collection of common ADB commands for app testing

【AI周报】AI与冷冻电镜揭示「原子级」NPC结构;清华、商汤提出「SIM」方法兼顾语义对齐与空间分辨能力

What is the relationship between precious metal silver and spot Silver
随机推荐
92. actual combat of completable future
English subtitle video translated into Chinese subtitles
Why is the trend chart of precious metal silver strong?
軟件測試英語常見詞匯
叶酸配体的金属有机骨架材料MOFs负载5-氟尿嘧啶,西达本胺,紫杉醇,阿霉素,柔红霉素,布洛芬,喜树碱,姜黄素,藤黄酸等小分子药物
SD3.0笔记
App test_ Summary of test points
Record scroll bar position, passive, scrolltop
扁平数据转tree与tree数据扁平化
20220610 星期五
What can the enterprise exhibition hall design bring to the enterprise?
金属有机框架MOF-Al(DIBA),MOF-Zr(DIBA),MOF-Fe(DIBA)包载姜黄素/羧苄西林/MTX甲氨蝶呤/紫杉醇PTX/阿霉素DOX/顺铂CDDP/CPT喜树碱等药物
Customized redistemplate in redis
Jetpack compose scaffold and topappbar (top navigation)
GCC C inline assembly
Setting access to win10 shared folder without verification
企业展厅设计能为企业带来什么?
What is the relationship between precious metal silver and spot Silver
Jetpack compose scaffold and bottomappbar (bottom navigation)
ADVANCE. AI CEO Shoudong will share the compliance of cross-border e-commerce using AI technology at the 2022 emerging market brands online Summit