2020-06-16(C语言)数据结构选择排序--简单选择排序

//简单选择排序

include

include

define MAXSIZE 100

typedef struct SqList
{
int r[MAXSIZE + 1];
int length;
} SqList;
void SelectSort(SqList *L) //对顺序表L做简单选择排序
{
int k, t;
for (int i = 1; i <= L->length; i++) //在L->r[i]到L->r[L.length]中选择关键字最小的记录
{
k = i;
for (int j = i + 1; j <= L->length; j++)
{
if (L->r[j] < L->r[k])
{
k = j; //k指向此趟排序中关键字最小的记录
}
}
if (k != i)
{
t = L->r[i];
L->r[i] = L->r[k];
L->r[k] = t; //交换r[i]与r[k]
}
}
}
int main()
{
SqList *L;
int i;
L = (SqList *)malloc(sizeof(SqList));
printf("请输入长度:");
scanf("%d", &L->length);
printf("请输入元素:");
for (i = 1; i <= L->length; i++)
{
scanf("%d", &L->r[i]);
}
printf("排序前:");
for (i = 1; i <= L->length; i++)
{
printf("%d ", L->r[i]);
}
printf("\n");
SelectSort(L);
printf("排序后:");
for (i = 1; i <= L->length; i++)
{
printf("%d ", L->r[i]);
}
printf("\n");
return 0;
}

你可能感兴趣的:(2020-06-16(C语言)数据结构选择排序--简单选择排序)