#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#define MAXSIZE 100
typedef int Status;
typedef int KeyType;
typedef int infoType;
typedef struct {
KeyType key;
infoType otherinfo;
}RedType;
typedef struct {
RedType r[MAXSIZE + 1];
int length;
}SqList;
void CreateSqList(SqList &L) {
int i,n;
printf("创建线性表:\n");
printf("输入数据个数:");
scanf("%d", &n);
L.length = n;
srand((unsigned)time(NULL));
for (i = 1; i <= L.length; i++)
L.r[i].key = rand() % 500;
}
void SimpleSelectSort(SqList &L) {
int i, j, k;
int temp;
for (i = 1; i <= L.length; i++) {
k = i;
for (j = i + 1; j <= L.length; j++)
if (L.r[j].key < L.r[k].key) k = j;
if (i != k) {
temp = L.r[i].key;
L.r[i].key = L.r[k].key;
L.r[k].key = temp;
}
}
}
void main() {
int i;
SqList L;
CreateSqList(L);
printf("随机生成数据:");
for (i = 1; i <= L.length; i++)
printf("%d ", L.r[i].key);
SimpleSelectSort(L);
printf("\n");
printf("输出选择排序后的数据:");
for(i=1;i<=L.length;i++)
printf("%d ", L.r[i].key);
getchar();
getchar();
}
简单选择排序
最新推荐文章于 2021-05-20 18:35:35 发布