当前位置:网站首页>冒泡排序法
冒泡排序法
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 ;
}
}
}
}
边栏推荐
- I Arouter framework analysis
- Even if you don't learn gradle, these common development operations are worth mastering
- Composition and simple classification of IP addresses
- Gx3001q UI instructions for upgrading 8-bit color to 16 bit color
- Easysearcher cannot find the IP address of easynvr hardware. How to solve it?
- Firewall and IP security policy configuration
- Parsing the implementation of easygbs compatible token as parameter passing
- 【owt】owt-client-native-p2p-e2e-test vs2017构建 4 : 第三方库的构建及链接p2pmfc.exe
- 【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现
- Initialize MySQL Gorm through yaml file
猜你喜欢

Using jhipster to build microservice architecture

直接插入排序

One of the touchdesigner uses - Download and install

Banknext microservice: a case study

Encryption related to returnee of national market supervision public service platform

Fetch request details

新版kali切换最高账户

【owt】owt-client-native-p2p-e2e-test vs2017构建 4 : 第三方库的构建及链接p2pmfc.exe

第一批00后下场求职:不要误读他们的“不一样”

【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现
随机推荐
Simple analysis of easygbs compatible with old version HLS streaming address method [with code]
【owt】owt-client-native-p2p-e2e-test vs2017构建 3 : 无 测试单元对比, 手动生成vs项目
Golang resource embedding scheme
数据加密技术之源代码加密
The difference between code39 and code93
How to save the model obtained from sklearn training? Just read this one
An implementation of warning bombing
centos7 安装 MySQL 及配置 innodb_ruby
One of the touchdesigner uses - Download and install
【LeetCode】23. 合并K个升序链表
Why don't I suggest you use "! = null" to judge empty space?
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
[burning] Tencent cloud high tech computing platform HTPC cloud elastic cluster release!
第一批00后下场求职:不要误读他们的“不一样”
Mybatties plus batch warehousing
Detailed discussion on modular architecture design of MCU firmware
Chapter IV open source projects and deployment
Heavyweight review: strategies for reliable fMRI measurements
How to store easydss version 3.0 video files in other free disks?
Salesforce heroku (V) application in salesforce (canvasapp)