(1)冒泡排序
头文件:
#define
MAXSIZE 20
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#define
EQ(a, b) ((a) == (b))
#define
LT(a, b) ((a) < (b))
#define
LQ(a, b) ((a) <= (b))
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
typedef
int
KeyType;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
typedef
int
InfoType;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
typedef
struct
...
{
KeyType key;
InfoType otherinfo;
}
RedType;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
typedef
struct
...
{
RedType r[MAXSIZE + 1];
int length;
}
SqList;
源文件:
#include
"
sort.h
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#include
"
stdio.h
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
void
init(SqList
&
s,
int
w[],
int
n)
...
{
s.length = n;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i = 1; i <= n; i++)...{
s.r[i].key = w[i - 1];
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
void
show(SqList s)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i = 1; i <= s.length; i++)...{
printf("%d ", s.r[i]);
}
printf(" ");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
void
bubbleSort(SqList
&
s)
...
{
int flag = 1;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i = s.length; i >= 1 && flag; i--)...{
flag = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int j = 1; j <= i; j++)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(LT(s.r[j].key, s.r[j - 1].key))...{
RedType rc = s.r[j - 1];
s.r[j - 1] = s.r[j];
s.r[j] = rc;
flag = 1;
}
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
void
main()
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
SqList s;
init(s, w, n);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
bubbleSort(s);
show(s);
}
程序执行结果:
13
27
38
49
49
65
76
97
Press any key to
continue
程序的复杂度为:O(n * n)。
(2)快速排序
源文件修改:
//
use r[low] as pivot to part from low to high.
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
int
partition(SqList
&
s,
int
low,
int
high)
...
{
s.r[0] = s.r[low];
KeyType key = s.r[low].key;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while(low < high)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while(low < high && s.r[high].key >= key)...{
high--;
}
s.r[low] = s.r[high];
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while(low < high && s.r[low].key <= key)...{
low++;
}
s.r[high] = s.r[0];
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return low;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
void
qSort(SqList
&
s,
int
low,
int
high)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(low < high)...{
int pivot = partition(s, low, high);
qSort(s, low, pivot - 1);
qSort(s, pivot + 1, high);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
void
quickSort(SqList
&
s)
...
{
qSort(s, 1, s.length);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
void
main()
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
SqList s;
init(s, w, n);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
quickSort(s);
show(s);
}
程序执行结果:
13
27
38
49
49
65
76
97
Press any key to
continue
算法的复杂度为:平均O(n * log(n)),最坏O(n * n)。