数据结构 排序 选择排序
来源:互联网 发布:2016外贸出口数据 编辑:程序博客网 时间:2024/05/22 12:17
(1)简单选择排序
头文件:
#define MAXSIZE 20
#define EQ(a, b) ((a) == (b))
#define LT(a, b) ((a) < (b))
#define LQ(a, b) ((a) <= (b))
typedef int KeyType;
typedef int InfoType;
typedef struct ...{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct ...{
RedType r[MAXSIZE + 1];
int length;
}SqList;
#define EQ(a, b) ((a) == (b))
#define LT(a, b) ((a) < (b))
#define LQ(a, b) ((a) <= (b))
typedef int KeyType;
typedef int InfoType;
typedef struct ...{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct ...{
RedType r[MAXSIZE + 1];
int length;
}SqList;
源文件:
#include "sort.h"
#include "stdio.h"
void init(SqList &s, int w[], int n)...{
s.length = n;
for(int i = 1; i <= n; i++)...{
s.r[i].key = w[i - 1];
}
}
void show(SqList s)...{
for(int i = 1; i <= s.length; i++)...{
printf("%d ", s.r[i]);
}
printf(" ");
}
int selectMin(SqList &s, int i)...{
int key = s.r[i].key;
int ret = i;
for(int k = i; k <= s.length; k++)...{
if(key > s.r[k].key)...{
key = s.r[k].key;
ret = k;
}
}
return ret;
}
void selectSort(SqList &s)...{
for(int i = 1; i <= s.length; i++)...{
int j = selectMin(s, i);
if(i != j)...{
RedType tmp = s.r[i];
s.r[i] = s.r[j];
s.r[j] = tmp;
}
}
}
void main()...{
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
SqList s;
init(s, w, n);
selectSort(s);
show(s);
}
#include "stdio.h"
void init(SqList &s, int w[], int n)...{
s.length = n;
for(int i = 1; i <= n; i++)...{
s.r[i].key = w[i - 1];
}
}
void show(SqList s)...{
for(int i = 1; i <= s.length; i++)...{
printf("%d ", s.r[i]);
}
printf(" ");
}
int selectMin(SqList &s, int i)...{
int key = s.r[i].key;
int ret = i;
for(int k = i; k <= s.length; k++)...{
if(key > s.r[k].key)...{
key = s.r[k].key;
ret = k;
}
}
return ret;
}
void selectSort(SqList &s)...{
for(int i = 1; i <= s.length; i++)...{
int j = selectMin(s, i);
if(i != j)...{
RedType tmp = s.r[i];
s.r[i] = s.r[j];
s.r[j] = tmp;
}
}
}
void main()...{
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
SqList s;
init(s, w, n);
selectSort(s);
show(s);
}
程序运行结果:
13 27 38 49 49 65 76 97
Press any key to continue
Press any key to continue
算法的复杂度为O(n * n),主要是关键字的比较。
(2)堆排序
头文件:
#define MAXSIZE 20
#define EQ(a, b) ((a) == (b))
#define LT(a, b) ((a) < (b))
#define LQ(a, b) ((a) <= (b))
typedef int KeyType;
typedef int InfoType;
typedef struct ...{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct ...{
RedType r[MAXSIZE + 1];
int length;
}HeapType;
#define EQ(a, b) ((a) == (b))
#define LT(a, b) ((a) < (b))
#define LQ(a, b) ((a) <= (b))
typedef int KeyType;
typedef int InfoType;
typedef struct ...{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct ...{
RedType r[MAXSIZE + 1];
int length;
}HeapType;
源文件:
#include "sort.h"
#include "stdio.h"
void init(HeapType &s, int w[], int n)...{
s.length = n;
for(int i = 1; i <= n; i++)...{
s.r[i].key = w[i - 1];
}
}
void show(HeapType s)...{
for(int i = 1; i <= s.length; i++)...{
printf("%d ", s.r[i]);
}
printf(" ");
}
// adjust the heap from s to m, to be a max heap.
void heapAdjust(HeapType &h, int s, int m)...{
RedType rc = h.r[s];
for(int j = 2 * s; j <= m; j *= 2)...{
if(j < m && LT(h.r[j].key, h.r[j + 1].key))...{
j++;
}
if(!LT(rc.key, h.r[j].key))...{
break;
}
h.r[s] = h.r[j];
s = j;
}
h.r[s] = rc;
}
void heapSort(HeapType &h)...{
//construct the heap
for(int i = h.length / 2; i > 0; i--)...{
heapAdjust(h, i, h.length);
}
for(i = h.length; i > 1; i--)...{
RedType rc = h.r[i];
h.r[i] = h.r[1];
h.r[1] = rc;
heapAdjust(h, 1, i - 1);
}
}
void main()...{
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
HeapType h;
init(h, w, n);
heapSort(h);
show(h);
}
#include "stdio.h"
void init(HeapType &s, int w[], int n)...{
s.length = n;
for(int i = 1; i <= n; i++)...{
s.r[i].key = w[i - 1];
}
}
void show(HeapType s)...{
for(int i = 1; i <= s.length; i++)...{
printf("%d ", s.r[i]);
}
printf(" ");
}
// adjust the heap from s to m, to be a max heap.
void heapAdjust(HeapType &h, int s, int m)...{
RedType rc = h.r[s];
for(int j = 2 * s; j <= m; j *= 2)...{
if(j < m && LT(h.r[j].key, h.r[j + 1].key))...{
j++;
}
if(!LT(rc.key, h.r[j].key))...{
break;
}
h.r[s] = h.r[j];
s = j;
}
h.r[s] = rc;
}
void heapSort(HeapType &h)...{
//construct the heap
for(int i = h.length / 2; i > 0; i--)...{
heapAdjust(h, i, h.length);
}
for(i = h.length; i > 1; i--)...{
RedType rc = h.r[i];
h.r[i] = h.r[1];
h.r[1] = rc;
heapAdjust(h, 1, i - 1);
}
}
void main()...{
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
HeapType h;
init(h, w, n);
heapSort(h);
show(h);
}
程序执行结果:
13 27 38 49 49 65 76 97
Press any key to continue
Press any key to continue
说明:
当n较小时,不值得用heapsort;最坏情况的复杂度为O(n * log(n))。
- 数据结构 排序 选择排序
- 【数据结构】:排序--选择排序
- 《数据结构--排序》之选择排序
- 数据结构排序之选择排序
- 数据结构(排序)--选择排序
- 数据结构-选择排序-堆排序
- 排序-选择排序-数据结构(31)
- 数据结构之选择排序
- 数据结构-选择类排序
- 数据结构算法----选择排序
- 【数据结构】选择排序
- 数据结构-选择排序算法
- java数据结构--选择排序
- C++数据结构--选择排序
- 数据结构 - 选择排序
- 【数据结构】选择排序
- 数据结构-直接选择排序
- 数据结构之选择排序
- 20070412
- 使用WebSphere 脚本工具 卸载应用
- 程序开发流程 2
- LINUX 下多进程调试方法
- 小小的总结一下网页间传值问题!
- 数据结构 排序 选择排序
- 网站设计服务流程
- 安全编程: 防止缓冲区溢出--防止如今最常见的程序缺陷
- 多媒体开发流程
- 安全编程: 警惕输入--找出并保护程序的入口
- c#程序控件命名
- 我的股票:2007/04/12
- 安全编程: 开发安全的程序--正确的理念是成功的一半
- 如何实现程序互斥运行