当前位置:网站首页>【冒泡排序的实现】
【冒泡排序的实现】
2022-06-11 01:43:00 【爱吃榴莲的喵星人】
1、冒泡排序的实现
提示:冒泡排序的时间复杂度为: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;
}
运行结果:
配上方便画图理解的图:
2、冒泡排序的函数实现
#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;
}
运行结果:
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- Koa2 learning notes
- [AI weekly] AI and freeze electron microscopy reveal the structure of "atomic level" NPC; Tsinghua and Shangtang proposed the "SIM" method, which takes into account semantic alignment and spatial reso
- SQL | calculate sum
- 为什么有的程序员能力一般却能拿到好offer?
- Closing method of SQL injection
- 软件测试是否需要掌握编程能力
- 【AI周报】AI与冷冻电镜揭示「原子级」NPC结构;清华、商汤提出「SIM」方法兼顾语义对齐与空间分辨能力
- SD3.0笔记
- Colab reported an error: importerror: cannot import name '_ check_ savefig_ extra_ args‘ from ‘matplotlib. backend_ bases‘
- Find - (sequential search)
猜你喜欢

Test questions and answers of 2022r1 quick opening pressure vessel operation certificate

421. maximum XOR value of two numbers in the array

如何3步精读《PMBOK指南》(经验+资料分享)

贵金属白银和现货白银之间是什么关系

koa2学习笔记

查看Redis内数据,除了命令行和客户端,你还有第三种选择
![[3.delphi common components] 6 scroll bar](/img/55/891e56de4500a9128ac89e3c5b1721.jpg)
[3.delphi common components] 6 scroll bar

Multilevel mesoporous organometallic framework material zif-8 loaded with lactic acid oxidase (LOD) / ferric oxide (Fe304) / doxorubicin / insulin /cas9 protein / metronidazole / emodin methyl ether

The interviewer of Tencent said that who knows the internal module index principle and performance optimization idea of MySQL architecture?

【并行与分布式系统】Cache学习
随机推荐
UI interaction
Unity3d model skin changing technology
Knowledge competition of safety production month -- how much do you know about new safety law
Is it appropriate for a 27 - year-old girl to change her career from zero to software testing?
优化调度(火电、风能、储能)【matlab代码实现】
English subtitle video translated into Chinese subtitles
Project records
Dynamically add attributes to objects
【无标题】
关于Set集合类你都知道什么?来自《卷Ⅰ》的灵魂提问
Record scroll bar position, passive, scrolltop
Jetpack compose scaffold and bottomappbar (bottom navigation)
【面试题 17.04. 消失的数字】
常见漏洞的防御措施整理
[untitled]
JS summary of math functions in math objects
Kotlin apply method
Project - redis message queue + worker thread fetches user operation logs and stores them (2)
项目 - Redis消息队列+工作线程取出用户操作日志并入库(二)
企业展厅设计能为企业带来什么?