当前位置:网站首页>冒泡排序法
冒泡排序法
2022-06-23 03:32:00 【@风景邮递Yuan】
排序用到的结构与函数
# define MAXSIZE 10
typedef struct {
/* 用 于 存 储 要 排 序 数 组 , r[0] 用 作 哨 兵 或 临 时 变 量 */
int r[ MAXSIZE + 1];
/* 用 于 记 录 顺 序 表 的 长 度 */
int length ;
} SqList ;
/* 交 换 L 中 数 组 r 的 下 标 为 i 和 j 的 值 */
void swap ( SqList *L, int i, int j){
int temp = L- >r[i];
L->r[i] = L- >r[j];
L->r[j] = temp ;
}冒泡排序初级版本
/* 对 顺 序 表 L 作 交 换 排 序 ( 冒 泡 排 序 初 级 版 ) */
void BubbleSort0 ( SqList *L){
int i, j;
for (i = 1; i < L- > length ; i++) {
for (j = i + 1; j <= L- > length ; j++) {
if (L- >r[i] > L->r[j]) {
/* 交 换L- >r[i] 与L->r[j] 的 值 */
swap (L, i, j) ;
}
}
}
}冒泡排序经典版本——两两相比

/* 对 顺 序 表 L 作 冒 泡 排 序 */
void BubbleSort ( SqList *L){
int i, j;
for (i = 1; i < L- > length ; i++) {
/* 注 意 j 是 从 后 往 前 循 环 */
for (j = L-> length - 1; j >= i;j - -) {
/* 若 前 者 大 于 后 者 ( 注 意 这 里 与 上 一 算 法 差 异 ) */
if (L- >r[j] > L->r[j + 1]) {
/* 交 换L- >r[j] 与L->r[j+1] 的 值 */
swap (L, j, j + 1) ;
}
}
}
}冒泡排序升级版本
如果嵌入的内部循环不需要交换,那么说明后面这段序列初始值就是排好的
void BubbleSort2 ( SqList *L){
int i, j;
Status flag = TRUE ; /* flag 用 来 作 为 标 记 */
/* 若 flag 为 true 说 明 有 过 数 据 交 换 , 否 则 停 止 循 环 */
for (i = 1; i < L- > length && flag ; i++) {
flag = FALSE ;/* 初 始 为 false */
for (j = L-> length - 1; j >= i; j - -) {
if (L- >r[j] > L->r[j + 1]) {
/* 交 换L- >r[j] 与L->r[j+1] 的 值 */
swap (L, j, j + 1) ;
/* 如 果 有 数 据 交 换 , 则 flag 为 true */
flag = TRUE ;
}
}
}
}
边栏推荐
- Best practices for building multi architecture mirrors
- 【LeetCode】翻转链表II
- ABCD identifier of SAP mm initial cycle count
- Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]
- The cloud disk has been offline from the internal machine, but the console uninstall failed
- I Arouter framework analysis
- Configuring multi cluster management using kubectl
- Build information query applet by using micro build
- New configuration of Alipay
- Official announcement! The Hong Kong Zhuhai Macao Bridge is finally here!
猜你喜欢

【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现

Jmeter- (V) simulated user concurrent login for interface test
![Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]](/img/81/4d3cb059f700dd9243645e64023be7.jpg)
Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]

One of the touchdesigner uses - Download and install

Fetch request details

centos7 安装 MySQL 及配置 innodb_ruby
![[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]](/img/f1/972a760459a6d599b5681aa634df09.jpg)
[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]

【二分】leetcode1011. Capacity To Ship Packages Within D Days

Using jhipster to build microservice architecture

【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行
随机推荐
Stress testing with locust on rainbow
[advanced Android] entrusted by kotlin
数据加密技术之源代码加密
1-1VMware介绍
Nature: correlation between oscillatory signals and gene expression supporting human episodic memory coding
【owt】owt-client-native-p2p-e2e-test vs2017构建 4 : 第三方库的构建及链接p2pmfc.exe
Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]
Installing serverstatus probe using pagoda
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]
Uploading logs using loghub log4j appender
【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM
Bi skills - authority control
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
Encryption related to returnee of national market supervision public service platform
Tcapulusdb Jun · industry news collection (IV)
Salesforce heroku (V) application in salesforce (canvasapp)
Apicloud native module, H5 module and multi terminal component tutorial
TRTC setaudioroute invalid problem
JS remove first character of string
What is the difference between comparator and comparable?