数据结构笔记 - 排序算法 最简单的排序实现 初级版的冒泡排序

发布于 2011-09-19 | 更新于 2020-09-20

这里介绍的是最简单的排序算法:初级版的冒泡排序算法,属于交换排序算法。

冒泡排序:是一种交换排序算法,它的基本思想是:比较两两相邻的元素,如果发现反序就交换位置,直到循环处理完所有的元素为止。

这里所介绍的初级版的冒泡排序算法的效率是非常低的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include

#define MAXSIZE 100 /* 待排序数组的大小 */

typedef struct
{
int r[MAXSIZE+1]; /* 待排序数组r,r[0]为哨兵或临时变量 */
int length; /* 待排序数组的长度,为了方便理解,不包含r[0]元素 */
}SortList;

/* 交换排序(初级版的冒泡排序算法) */
void BubbleSort01(SortList *L)
{
int i,j;
int temp;
for(i=1;ilength;i++)
{
for(j=i+1;jlength;j++)
{
if(L->r[i]>L->r[j])
{
/* 交换r[i]和r[j]的值 */
temp=L->r[i];
L->r[i]=L->r[j];
L->r[j]=temp;
}
}
}
}

本文作者: arthinking

本文链接: https://www.itzhai.comdata-structure-notes-the-simplest-sort-sorting-algorithm-to-achieve-the-primary-version-of-the-bubble-sort.html

版权声明: 版权归作者所有,未经许可不得转载,侵权必究!联系作者请加公众号。

×
IT宅

关注公众号及时获取网站内容更新。