本题要求实现基于顺序表的直接选择排序算法,要求打印出每一趟的排序结果。顺序表的结构体定义如下
typedef int DataType;
#define LISTSIZE 100
typedef struct
{
DataType list[LISTSIZE];
int length;
}SqList;
函数接口定义:
void SelectSort(SqList *L)
L
是用户传入的参数,代表待排序的顺序表,也是排序后返回的顺序表。要求打印出每一趟的排序结果。
裁判测试程序样例:
#include <stdio.h>
int InitList(SqList *L); /* 初始化顺序表 */
int ListInsert(SqList *L, int pos, DataType item); /* 插入结点,细节在此不表 */
int TraverList(SqList L); /* 打印顺序表,细节在此不表,可直接调用 */
void swap(SqList *L, int i, int j) /* 交换顺序表里下标为i和j的两个结点 */
{
DataType temp = L->list[i];
L->list[i] = L->list[j];
L->list[j] = temp;
}
void SelectSort(SqList *L); /* 本题要求函数 */
int main()
{
SqList L;
DataType x;
char ch;
int pos = 1;
InitList(&L);
do
{
scanf("%d",&x);
ListInsert( &L , pos++ , x );
}while ((ch=getchar())!='\n');
SelectSort(&L);
printf("The sorted List is\n");
TraverList(L);
return 0;
}
输入样例:
23 45 12 20 31
输出样例:
12 45 23 20 31
12 20 23 45 31
12 20 23 45 31
12 20 23 31 45
The sorted List is
12 20 23 31 45