函数题:基于顺序表的直接插入排序【有题解视频】
本题要求实现基于顺序表的直接插入排序算法,要求打印出每一趟的排序结果。顺序表的结构体定义如下
c
typedef int DataType;
#define LISTSIZE 100
typedef struct
{
DataType list[LISTSIZE];
int length;
}SqList;
### 函数接口定义:
c
void InsertSort(SqList *L)
L 是用户传入的参数,代表待排序的顺序表,也是排序后返回的顺序表。要求打印出每一趟的排序结果。
### 裁判测试程序样例:
c
#include <stdio.h>
typedef int DataType; // 定义具体数据类型
#define LISTSIZE 100 // LISTSIZE 表示顺序表可能的最大数据元素数目
/* 顺序表存储结构 */
typedef struct
{
DataType list[LISTSIZE];
int length;
}SqList;
/* 初始化顺序表 */
int InitList(SqList *L) // L为指向顺序表的指针
{
L->length = 0;
return 1;
}
/* 求顺序表表长 */
int ListLenth(SqList L) // L为顺序表
{
return L.length;
}
/* 判断顺序表是否为空 */
int ListEmpty(SqList L) // L为顺序表
{
if (L.length <= 0)
{
return 1;
}
else
{
return 0;
}
}
/* 向顺序表插入元素 */
int ListInsert(SqList *L, int pos, DataType item)
{ // L为指向顺序表的指针,pos为插入位置,item为待插入的数据元素
int i;
if (L -> length >= LISTSIZE) // 判断顺序表是否已满
{
printf("顺序表已满,无法插入\n");
return 0;
}
if (pos <= 0 || pos > L -> length + 1)
{ // 检查元素插入位置是否在顺序表里
printf("插入位置不合法\n");
return 0;
}
for (i = L -> length - 1; i >= pos - 1; i--)
{ // 移动数据元素
L -> list[i + 1] = L -> list[i];
}
L -> list[pos - 1] = item; // 插入元素
L -> length++; // 表长加一
return 1;
}
/* 遍历顺序表 */
int TraverList(SqList L) // L为顺序表
{
int i;
for(i = 0; i < L.length; i++)
{
printf("%d ", L.list[i]);
}
printf("\n");
return 1;
}
void InsertSort(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');
InsertSort(&L);
printf("The sorted List is\n");
TraverList(L);
return 0;
}
/* 请在这里填写答案 */
### 输入样例:
in
23 45 12 20 31
### 输出样例:
out
23 45 12 20 31
12 23 45 20 31
12 20 23 45 31
12 20 23 31 45
The sorted List is
12 20 23 31 45
答案:若无答案欢迎评论
#写在前面的话#
*提供题解视频,是为了帮助大家掌握算法的思想,在今后的编程、考试、面试中,换了语言,换了编译器,换了开发平台,都能重现这种思想,从而编写出任意语言任意编译器任意开发平台下面的这种算法的代码来,所以对于一打开题解报告,就直接拖到视频最后看参考代码的做法,我们非常不赞同,也不符合我们制作PPT录制讲解视频的初衷,希望大家体谅我们的苦心。*
https://www.bilibili.com/video/BV1X3411p7bR/

c
typedef int DataType;
#define LISTSIZE 100
typedef struct
{
DataType list[LISTSIZE];
int length;
}SqList;
### 函数接口定义:
c
void InsertSort(SqList *L)
L 是用户传入的参数,代表待排序的顺序表,也是排序后返回的顺序表。要求打印出每一趟的排序结果。
### 裁判测试程序样例:
c
#include <stdio.h>
typedef int DataType; // 定义具体数据类型
#define LISTSIZE 100 // LISTSIZE 表示顺序表可能的最大数据元素数目
/* 顺序表存储结构 */
typedef struct
{
DataType list[LISTSIZE];
int length;
}SqList;
/* 初始化顺序表 */
int InitList(SqList *L) // L为指向顺序表的指针
{
L->length = 0;
return 1;
}
/* 求顺序表表长 */
int ListLenth(SqList L) // L为顺序表
{
return L.length;
}
/* 判断顺序表是否为空 */
int ListEmpty(SqList L) // L为顺序表
{
if (L.length <= 0)
{
return 1;
}
else
{
return 0;
}
}
/* 向顺序表插入元素 */
int ListInsert(SqList *L, int pos, DataType item)
{ // L为指向顺序表的指针,pos为插入位置,item为待插入的数据元素
int i;
if (L -> length >= LISTSIZE) // 判断顺序表是否已满
{
printf("顺序表已满,无法插入\n");
return 0;
}
if (pos <= 0 || pos > L -> length + 1)
{ // 检查元素插入位置是否在顺序表里
printf("插入位置不合法\n");
return 0;
}
for (i = L -> length - 1; i >= pos - 1; i--)
{ // 移动数据元素
L -> list[i + 1] = L -> list[i];
}
L -> list[pos - 1] = item; // 插入元素
L -> length++; // 表长加一
return 1;
}
/* 遍历顺序表 */
int TraverList(SqList L) // L为顺序表
{
int i;
for(i = 0; i < L.length; i++)
{
printf("%d ", L.list[i]);
}
printf("\n");
return 1;
}
void InsertSort(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');
InsertSort(&L);
printf("The sorted List is\n");
TraverList(L);
return 0;
}
/* 请在这里填写答案 */
### 输入样例:
in
23 45 12 20 31
### 输出样例:
out
23 45 12 20 31
12 23 45 20 31
12 20 23 45 31
12 20 23 31 45
The sorted List is
12 20 23 31 45
答案:若无答案欢迎评论
#写在前面的话#
*提供题解视频,是为了帮助大家掌握算法的思想,在今后的编程、考试、面试中,换了语言,换了编译器,换了开发平台,都能重现这种思想,从而编写出任意语言任意编译器任意开发平台下面的这种算法的代码来,所以对于一打开题解报告,就直接拖到视频最后看参考代码的做法,我们非常不赞同,也不符合我们制作PPT录制讲解视频的初衷,希望大家体谅我们的苦心。*
https://www.bilibili.com/video/BV1X3411p7bR/
