当前位置:网站首页>【冒泡排序的实现】
【冒泡排序的实现】
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;
}
运行结果:
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- 叶酸配体的金属有机骨架材料MOFs负载5-氟尿嘧啶,西达本胺,紫杉醇,阿霉素,柔红霉素,布洛芬,喜树碱,姜黄素,藤黄酸等小分子药物
- 常见漏洞的防御措施整理
- 2022 high altitude installation, maintenance and removal of simulated examination platform of theoretical question bank
- Project records
- Project load failed
- 889. 根据前序和后序遍历构造二叉树
- Stc8a8k64d4 EEPROM read / write failure
- 1031. maximum sum of two non overlapping subarrays
- What do you know about the set class? Soul questions from Volume I
- When a logical deletion encounters a unique index, what are the problems and solutions?
猜你喜欢

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

How to guarantee the data quality of data warehouse?

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

Navicat Premium 15 工具自动被杀毒防护软件删除解决方法

Li Kou brushing questions - hash table

MySQL备份与恢复

关于Set集合类你都知道什么?来自《卷Ⅰ》的灵魂提问

What do you know about the set class? Soul questions from Volume I

软件测试英语常见词汇
![[C language] storage of data in memory -1 plastic](/img/4a/24c1bb4743bd4ae965ed88f333f2fe.jpg)
[C language] storage of data in memory -1 plastic
随机推荐
Executeineditmode property details
MySQL backup and recovery
Colab报错:ImportError: cannot import name ‘_check_savefig_extra_args‘ from ‘matplotlib.backend_bases‘
心态不能崩......
app 测试 常用 adb 命令集合
动态给对象添加属性
During SSH configuration key login, you need to pay attention to whether the private key is set with a password
叶酸配体的金属有机骨架材料MOFs负载5-氟尿嘧啶,西达本胺,紫杉醇,阿霉素,柔红霉素,布洛芬,喜树碱,姜黄素,藤黄酸等小分子药物
Principle of everything for fast search
APP测试_测试点总结
Large screen - full screen, exit full screen
Cyclodextrin metal organic framework( β- Cd-mof) loaded with dimercaptosuccinic acid / emodin / quercetin / sucralose / diflunisal / omeprazole (OME)
Jetpack compose scaffold and topappbar (top navigation)
MySQL备份与恢复
How to read PMBOK guide in 3 steps (experience + data sharing)
如何保障数仓数据质量?
【无标题】
年金保險理財產品可以複利嗎?利率是多少?
SQL | 外连接
[3.delphi common components] 5 List class component