当前位置:网站首页>冒泡排序法
冒泡排序法
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 ;
}
}
}
}
边栏推荐
- Mybatties plus batch warehousing
- Detailed discussion on modular architecture design of MCU firmware
- Svn local computer storage configuration
- [quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]
- JS array de duplication, removing the same value
- Downloading sqlserver versions (2016-2019)
- Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
- Build information query applet by using micro build
- 【LeetCode】翻转链表II
- Enterprise official website applet building tutorial
猜你喜欢

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

【二分】leetcode1011. Capacity To Ship Packages Within D Days
![Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]](/img/84/192d152ceb760264b6b555b321f129.jpg)
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]

R tree of search tree

mysql常用指令
![Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]](/img/4b/dd272f98b89a157180bf68570d2763.jpg)
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]

【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM

嵌入式软件测试工具TPT18更新全解析

【贪心】leetcode991. Broken Calculator

centos7 安装 MySQL 及配置 innodb_ruby
随机推荐
How to store easydss version 3.0 video files in other free disks?
数据加密技术之源代码加密
How to save the model obtained from sklearn training? Just read this one
Summary of some precautions and problems in the use of tars framework (mengxinxiang)
Engineer culture: should the company buy genuine software
Easysearcher cannot find the IP address of easynvr hardware. How to solve it?
Firewall and IP security policy configuration
This point (II)
d重载嵌套函数
第一批00后下场求职:不要误读他们的“不一样”
YouTube security scenarios
Apicloud native module, H5 module and multi terminal component tutorial
Get method of fetch request and data of formdata type submitted by post
线上MySQL的自增id用尽怎么办?
【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现
C. Differential Sorting
[metauniverse 7ai rope skipping] how is this app light application realized? What are the application scenarios?
Simply use the pagoda to build WordPress
【二分】leetcode1011. Capacity To Ship Packages Within D Days
What are the advantages and difficulties of introducing AI into ISP Technology