1、排序算法练习
分别使用直接插入排序(课本P236-237)、直接选择排序(课本P241-242)、冒泡排序(课本P248)、快速排序(课本P249-250)算法,完成对学生信息(P36学生结构体)的排序(按学号或按年龄或按成绩)。
提示:注意排序关键字key。
2、查找算法练习
参考课本P262代码,改变main()中数据为{{710,342,45,686,6,841,686,134,686,264},10},找出所有686的位置。
理解使用顺序表进行查找的过程;使用有序顺序表进行顺序查找以及二分查找的过程。
1.排序算法练习:
(一)用快速排序对学生信息排序:
头文件:SeqList.h
#include<stdio.h>
#define MaxSize 100
typedef struct Student
{
long number;
char name[10];
char sex[3];
int age;
double score;
}StudentType;
typedef StudentType DataType;
typedef struct
{
DataType list[MaxSize];
int size;
}SeqList;
void ListInitiate(SeqList* L)
{
L->size = 0;
}
int ListLength(SeqList L)
{
return L.size;
}
int ListInsert(SeqList* L, int i, DataType x)
{
int j;
if (L->size >= MaxSize)
{
printf("数组已满无法插入!\n");
return 0;
}
else if (i<0 || i>L->size)
{
printf("参数i不合法!\n");
return 0;
}
else
{
for (j = L->size; j > i; j--)
L->list[j] = L->list[j - 1];
L->list[i] = x;
L->size++;
return 1;
}
}
int ListDelete(SeqList* L, int i, DataType* x)
{
int j;
if (L->size <= 0)
{
printf("顺序表已空无数据可删!\n");
return 0;
}
else if (i<0 || i>L->size - 1)
{
printf("参数i不合法");
return 0;
}
else
{
*x = L->list[i];
for (j = i + 1; j <= L->size - 1; j++)
L->list[j - 1] = L->list[j];
L->size--;
return 1;
}
}
int ListGet(SeqList L, int i, DataType* x)
{
if (i<0 || i>L.size - 1)
{
printf("参数i不合法!\n");
return 0;
}
else
{
*x = L.list[i];
return 1;
}
}
源文件:用快速排序对学生信息排序.c
#include"SeqList.h"
void QuickSort(DataType a[],int low,int high)
{
int i=low,j=high;
DataType temp=a[low];
while(i<j)
{
while(i<j&&temp.score <=a[j].score)
j--;
if(i<j)
{
a[i]=a[j];
i++;
}
while (i < j && a[i].score < temp.score)
i++;
if(i<j)
{
a[j]=a[i];
j--;
}
}
a[i]=temp;
if(low<i)
QuickSort(a,low,i-1);
if(i<high)
QuickSort(a,j+1,high);
}
int main()
{
int i;
StudentType x[3] = { {2000001,"张三","男",20,92.8},
{2000002,"李四","男",40,99.6},
{2000003,"王五","女",30,79} };
QuickSort(x,0,2);
for (i = 0; i < 3; i++)
{
printf("%ld %s %s %d %.2f\n", x[i].number, x[i].name, x[i].sex, x[i].age, x[i].score);
}
return 0;
}
(二)用冒泡排序对学生信息排序:
头文件:SeqList.h
#include<stdio.h>
#define MaxSize 100
typedef struct Student
{
long number;
char name[10];
char sex[3];
int age;
double score;
}StudentType;
typedef StudentType DataType;
typedef struct
{
DataType list[MaxSize];
int size;
}SeqList;
void ListInitiate(SeqList* L)
{
L->size = 0;
}
int ListLength(SeqList L)
{
return L.size;
}
int ListInsert(SeqList* L, int i, DataType x)
{
int j;
if (L->size >= MaxSize)
{
printf("数组已满无法插入!\n");
return 0;
}
else if (i<0 || i>L->size)
{
printf("参数i不合法!\n");
return 0;
}
else
{
for (j = L->size; j > i; j--)
L->list[j] = L->list[j - 1];
L->list[i] = x;
L->size++;
return 1;
}
}
int ListDelete(SeqList* L, int i, DataType* x)
{
int j;
if (L->size <= 0)
{
printf("顺序表已空无数据可删!\n");
return 0;
}
else if (i<0 || i>L->size - 1)
{
printf("参数i不合法");
return 0;
}
else
{
*x = L->list[i];
for (j = i + 1; j <= L->size - 1; j++)
L->list[j - 1] = L->list[j];
L->size--;
return 1;
}
}
int ListGet(SeqList L, int i, DataType* x)
{
if (i<0 || i>L.size - 1)
{
printf("参数i不合法!\n");
return 0;
}
else
{
*x = L.list[i];
return 1;
}
}
源文件:用冒泡排序对学生信息排序.c
#include"SeqList.h"
void BubbleSort(DataType a[], int n)
{
int i, j, flag = 1;
DataType temp;
for (i = 1; i < n && flag == 1; i++)
{
flag = 0;
for (j = 0; j < n - i; j++)
{
if (a[j].score > a[j + 1].score)
{
flag = 1;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
int main()
{
int i;
StudentType x[3] = { {2000001,"张三","男",20,92.8},
{2000002,"李四","男",40,99.6},
{2000003,"王五","女",30,79} };
BubbleSort(x, 3);
for (i = 0; i < 3; i++)
{
printf("%ld %s %s %d %.2f\n", x[i].number, x[i].name, x[i].sex, x[i].age, x[i].score);
}
return 0;
}
(三)用直接插入排序对学生信息排序:
头文件:SeqList.h
#include<stdio.h>
#define MaxSize 100
typedef struct Student
{
long number;
char name[10];
char sex[3];
int age;
double score;
}StudentType;
typedef StudentType DataType;
typedef struct
{
DataType list[MaxSize];
int size;
}SeqList;
void ListInitiate(SeqList* L)
{
L->size = 0;
}
int ListLength(SeqList L)
{
return L.size;
}
int ListInsert(SeqList* L, int i, DataType x)
{
int j;
if (L->size >= MaxSize)
{
printf("数组已满无法插入!\n");
return 0;
}
else if (i<0 || i>L->size)
{
printf("参数i不合法!\n");
return 0;
}
else
{
for (j = L->size; j > i; j--)
L->list[j] = L->list[j - 1];
L->list[i] = x;
L->size++;
return 1;
}
}
int ListDelete(SeqList* L, int i, DataType* x)
{
int j;
if (L->size <= 0)
{
printf("顺序表已空无数据可删!\n");
return 0;
}
else if (i<0 || i>L->size - 1)
{
printf("参数i不合法");
return 0;
}
else
{
*x = L->list[i];
for (j = i + 1; j <= L->size - 1; j++)
L->list[j - 1] = L->list[j];
L->size--;
return 1;
}
}
int ListGet(SeqList L, int i, DataType* x)
{
if (i<0 || i>L.size - 1)
{
printf("参数i不合法!\n");
return 0;
}
else
{
*x = L.list[i];
return 1;
}
}
源文件:用直接插入排序对学生信息排序.c
#include"SeqList.h"
void InsertSort(DataType a[], int n)
{
int i, j;
DataType temp;
for (i = 0; i < n - 1; i++)
{
temp = a[i + 1];
j = i;
while (j > -1 && temp.score < a[j].score)
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
}
int main()
{
int i;
StudentType x[3] = { {2000001,"张三","男",20,92.8},
{2000002,"李四","男",40,99.6},
{2000003,"王五","女",30,79} };
InsertSort(x, 3);
for (i = 0; i < 3; i++)
{
printf("%ld %s %s %d %.2f\n", x[i].number, x[i].name, x[i].sex, x[i].age, x[i].score);
}
return 0;
}
(四)用直接选择排序对学生信息排序:
头文件:SeqList.h
#include<stdio.h>
#define MaxSize 100
typedef struct Student
{
long number;
char name[10];
char sex[3];
int age;
double score;
}StudentType;
typedef StudentType DataType;
typedef struct
{
DataType list[MaxSize];
int size;
}SeqList;
void ListInitiate(SeqList* L)
{
L->size = 0;
}
int ListLength(SeqList L)
{
return L.size;
}
int ListInsert(SeqList* L, int i, DataType x)
{
int j;
if (L->size >= MaxSize)
{
printf("数组已满无法插入!\n");
return 0;
}
else if (i<0 || i>L->size)
{
printf("参数i不合法!\n");
return 0;
}
else
{
for (j = L->size; j > i; j--)
L->list[j] = L->list[j - 1];
L->list[i] = x;
L->size++;
return 1;
}
}
int ListDelete(SeqList* L, int i, DataType* x)
{
int j;
if (L->size <= 0)
{
printf("顺序表已空无数据可删!\n");
return 0;
}
else if (i<0 || i>L->size - 1)
{
printf("参数i不合法");
return 0;
}
else
{
*x = L->list[i];
for (j = i + 1; j <= L->size - 1; j++)
L->list[j - 1] = L->list[j];
L->size--;
return 1;
}
}
int ListGet(SeqList L, int i, DataType* x)
{
if (i<0 || i>L.size - 1)
{
printf("参数i不合法!\n");
return 0;
}
else
{
*x = L.list[i];
return 1;
}
}
源文件:用直接选择排序对学生信息排序.c
#include"SeqList.h"
void SelectSort(DataType a[], int n)
{
int i, j, small;
DataType temp;
for (i = 0; i < n - 1; i++)
{
small = i;
for (j = i + 1; j < n; j++)
{
if (a[j].score < a[small].score)
small = j;
}
if (small != i)
{
temp = a[i];
a[i] = a[small];
a[small] = temp;
}
}
}
int main()
{
int i;
StudentType x[3] = { {2000001,"张三","男",20,92.8},
{2000002,"李四","男",40,99.6},
{2000003,"王五","女",30,79} };
SelectSort(x, 3);
for (i = 0; i < 3; i++)
{
printf("%ld %s %s %d %.2f\n", x[i].number, x[i].name, x[i].sex, x[i].age, x[i].score);
}
return 0;
}
2.查找算法练习
(一)二分查找多个686:
头文件:SeqList.h
#include<stdio.h>
#define MaxSize 100
typedef int KeyType;
typedef struct
{
KeyType key;
}DataType;
typedef struct
{
DataType list[MaxSize];
int size;
}SeqList;
void ListInitiate(SeqList* L)
{
L->size = 0;
}
int ListLength(SeqList L)
{
return L.size;
}
int ListInsert(SeqList* L, int i, DataType x)
{
int j;
if (L->size >= MaxSize)
{
printf("数组已满无法插入!\n");
return 0;
}
else if (i<0 || i>L->size)
{
printf("参数i不合法!\n");
return 0;
}
else
{
for (j = L->size; j > i; j--)
L->list[j] = L->list[j - 1];
L->list[i] = x;
L->size++;
return 1;
}
}
int ListDelete(SeqList* L, int i, DataType* x)
{
int j;
if (L->size <= 0)
{
printf("顺序表已空无数据可删!\n");
return 0;
}
else if (i<0 || i>L->size - 1)
{
printf("参数i不合法");
return 0;
}
else
{
*x = L->list[i];
for (j = i + 1; j <= L->size - 1; j++)
L->list[j - 1] = L->list[j];
L->size--;
return 1;
}
}
int ListGet(SeqList L, int i, DataType* x)
{
if (i<0 || i>L.size - 1)
{
printf("参数i不合法!\n");
return 0;
}
else
{
*x = L.list[i];
return 1;
}
}
源文件:二分查找多个686.c
#include"SeqList.h"
void Binarysearch(SeqList S, DataType x, int l, int r)
{
if (l > r)
return;
int mid = (l + r) / 2;
if (S.list[mid].key == x.key)
printf("该数据元素出现过的位置有:%d\n", mid);
Binarysearch(S, x, l, mid - 1);
Binarysearch(S, x, mid + 1, r);
}
int main()
{
SeqList myS = { {710,342,45,686,6,841,686,134,686,264},10 };
DataType x = { 686 };
Binarysearch(myS, x, 0, 9);
return 0;
}
(二)顺序查找多个686:
头文件:SeqList.h
#include<stdio.h>
#define MaxSize 100
typedef int KeyType;
typedef struct
{
KeyType key;
}DataType;
typedef struct
{
DataType list[MaxSize];
int size;
}SeqList;
void ListInitiate(SeqList* L)
{
L->size = 0;
}
int ListLength(SeqList L)
{
return L.size;
}
int ListInsert(SeqList* L, int i, DataType x)
{
int j;
if (L->size >= MaxSize)
{
printf("数组已满无法插入!\n");
return 0;
}
else if (i<0 || i>L->size)
{
printf("参数i不合法!\n");
return 0;
}
else
{
for (j = L->size; j > i; j--)
L->list[j] = L->list[j - 1];
L->list[i] = x;
L->size++;
return 1;
}
}
int ListDelete(SeqList* L, int i, DataType* x)
{
int j;
if (L->size <= 0)
{
printf("顺序表已空无数据可删!\n");
return 0;
}
else if (i<0 || i>L->size - 1)
{
printf("参数i不合法");
return 0;
}
else
{
*x = L->list[i];
for (j = i + 1; j <= L->size - 1; j++)
L->list[j - 1] = L->list[j];
L->size--;
return 1;
}
}
int ListGet(SeqList L, int i, DataType* x)
{
if (i<0 || i>L.size - 1)
{
printf("参数i不合法!\n");
return 0;
}
else
{
*x = L.list[i];
return 1;
}
}
源文件:顺序查找多个686.c
#include"SeqList.h"
void SeqSearch(SeqList S, DataType x)
{
int i = 0, flag = 1;
while (i < S.size)
{
i++;
if (S.list[i].key == x.key)
{
printf("该数据元素出现过的位置有:%d\n", i);
flag = 0;
}
}
if (flag)
printf("查找失败");
}
int main()
{
SeqList myS = { {710,342,45,686,6,841,686,134,686,264},10 };
DataType x = { 686 };
SeqSearch(myS, x);
return 0;
}