C语言---数据结构实验---查找算法的实现---实现给定数组的快速排序

写在前面

  1. 本篇实现也全部通过动态内存实现

  2. 快速排序是通过递归或非递归实现的,其中对于单趟PartSort也有三种不同的算法,这三种不同的算法效率没有差异,通常是通过递归实现快速排序,非递归需要借助栈或队列,这里展示的是递归版、前后指针法实现快速排序,如果有其他需求可以看此文章自行寻找所需算法

数据结构—手撕图解排序(含动图演示)

查找算法的实现

题目描述

内容要求:

  1. 创建如下查找表:

学号 姓名 高等数学 C程序设计 数据结构
1301 白雪 88 92 89
1302 常亮 73 77 68
1303 冯玲 80 82 75
1304 李斌 90 89 90
1305 任芳 60 71 58
1306 史可 78 88 79
1307 吴伟 95 89 90
1308 杨宁 75 86 84
1309 张华 83 79 80
1310 郑新 58 62 60

  1. 使用顺序查找算法,从查找表中查找姓名为任芳和赵斌的学生。若查找成功,则给出该生相关信息,若查找不成功,则给出相应提示信息。

  2. 使用二分法查找算法,从查找表中查找数据结构成绩为68和90的学生。若查找成功,则给出该生相关信息,若查找不成功,则给出相应提示信息。

题目分析

手动输入数据法

#include<stdio.h>
#include<string.h>
#include<stdlib.h>


typedef struct
{
    
    
	int num;
	char name[50];
	int score_G;
	int score_C;
	int score_S;
}SLDataType;

typedef struct
{
    
    
	SLDataType* elem;
	int size;
	int capacity;
}SSTable;

void CheckCapacity(SSTable* ps)
{
    
    
	if (ps->capacity <= ps->size)
	{
    
    
		int newcapacity = ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->elem, sizeof(SLDataType) * newcapacity);
		if (tmp == NULL)
		{
    
    
			perror("realloc fail");
			return;
		}
		ps->capacity = newcapacity;
		ps->elem = tmp;
	}
}

void SeqListInit(SSTable* ps)
{
    
    
	ps->size = 0;
	ps->capacity = 8;
	ps->elem = (SLDataType*)malloc(ps->capacity * sizeof(SLDataType));
	if (ps->elem == NULL)
	{
    
    
		perror("malloc fail");
		return;
	}
}

void CreateList(SSTable* ps)
{
    
    
	printf("输入线性表的长度->");
	scanf("%d", &ps->size);
	CheckCapacity(ps);
	printf("请输入线性表的内容:");
	printf("\n学号  姓名  高等数学  C程序设计  数据结构\n");
	for (int i = 0; i < ps->size; i++)
	{
    
    
		scanf("%d", &ps->elem[i].num);
		scanf("%s", ps->elem[i].name);
		scanf("%d", &ps->elem[i].score_G);
		scanf("%d", &ps->elem[i].score_C);
		scanf("%d", &ps->elem[i].score_S);
	}
}

void Search_name(SSTable* ps)
{
    
    
	int i = 0;
	char name[50];
	printf("请输入要查找的姓名->");
	scanf("%s", name);
	for (i = 0; i < ps->size; i++)
	{
    
    
		if (strcmp(name, ps->elem[i].name) == 0)
		{
    
    
			printf("%d", ps->elem[i].num);
			printf("%5s", ps->elem[i].name);
			printf("%5d", ps->elem[i].score_G);
			printf("%5d", ps->elem[i].score_C);
			printf("%5d", ps->elem[i].score_S);
			break;
		}
		else
		{
    
    
			printf("无相关信息!");
		}
	}
}

void Search_score(SSTable* ps)
{
    
    
	int score_S;
	printf("\n请输入要查找的数据结构成绩:\n");
	scanf("%d", &score_S);
	int low = 0;
	int high = ps->size - 1;
	int mid = (low + high) / 2;
	while (low <= high)
	{
    
    
		mid = (low + high) / 2;

		if (score_S == ps->elem[mid].score_S)
		{
    
    
			printf("%4d", ps->elem[mid].num);
			printf("%5s", ps->elem[mid].name);
			printf("%5d", ps->elem[mid].score_G);
			printf("%5d", ps->elem[mid].score_C);
			printf("%5d", ps->elem[mid].score_S);
			break;
		}
		else if (score_S < ps->elem[mid].score_S)
		{
    
    
			high = mid - 1;
		}
		else
		{
    
    
			low = mid + 1;
		}

	}
	if (high < low)
	{
    
    
		printf("无相关信息!");
	}
}

void sort_score_S(SSTable* ps)
{
    
    
	int i, j;
	SLDataType temp;
	for (i = 0; i < ps->size - 1; i++)
	{
    
    
		for (j = 0; j < ps->size - 1 - i; j++)
		{
    
    
			if (ps->elem[j + 1].score_S > ps->elem[j].score_S)
			{
    
    
				temp = ps->elem[j + 1];
				ps->elem[j + 1] = ps->elem[j];
				ps->elem[j] = temp;
			}
		}
	}
	printf("\n将数据结构成绩按由小到大进行排序:\n");
	for (i = 0; i < ps->size; i++)
	{
    
    
		printf("%5d", ps->elem[i].num);
		printf("%5s", ps->elem[i].name);
		printf("%5d", ps->elem[i].score_G);
		printf("%5d", ps->elem[i].score_C);
		printf("%5d", ps->elem[i].score_S);
		printf("\n");
	}
}

void menu()
{
    
    
	printf("\n---------------1.按姓名查找---------------\n");
	printf("\n-----------2.按数据结构成绩查找------------\n");
	printf("\n---------------0.退出查找!---------------\n");
}

int main()
{
    
    
	SSTable s;
	SeqListInit(&s);
	int input = 0;
	menu();
	CreateList(&s);
	sort_score_S(&s);
	do
	{
    
    
		menu();
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			Search_name(&s);
			break;
		case 2:
			Search_score(&s);
			break;
		case 0:
			break;
		default:
			printf("该关键字非法!");
			break;
		}
	} while (input);
	return 0;
}

直接读取文件写法

直接读取文件需要在根目录下加入test.txt
内容为表格内容

#include<stdio.h>
#include<string.h>
#include<stdlib.h>


typedef struct
{
    
    
	int num;
	char name[50];
	int score_G;
	int score_C;
	int score_S;
}SLDataType;

typedef struct
{
    
    
	SLDataType* elem;
	int size;
	int capacity;
}SSTable;

void CheckCapacity(SSTable* ps)
{
    
    
	if (ps->capacity <= ps->size)
	{
    
    
		int newcapacity = ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->elem, sizeof(SLDataType) * newcapacity);
		if (tmp == NULL)
		{
    
    
			perror("realloc fail");
			return;
		}
		ps->capacity = newcapacity;
		ps->elem = tmp;
	}
}

void SeqListInit(SSTable* ps)
{
    
    
	ps->size = 0;
	ps->capacity = 8;
	ps->elem = (SLDataType*)malloc(ps->capacity * sizeof(SLDataType));
	if (ps->elem == NULL)
	{
    
    
		perror("malloc fail");
		return;
	}
}

void CreateList(SSTable* ps)
{
    
    
	FILE* pf = fopen("data.txt", "r");
	if (pf == NULL)
	{
    
    
		perror("fopen fail");
		return;
	}
	ps->size = 10;
	CheckCapacity(ps);
	printf("\n学号  姓名  高等数学  C程序设计  数据结构\n");
	for (int i = 0; i < ps->size; i++)
	{
    
    
		fscanf(pf,"%d", &ps->elem[i].num);
		fscanf(pf,"%s", ps->elem[i].name);
		fscanf(pf,"%d", &ps->elem[i].score_G);
		fscanf(pf,"%d", &ps->elem[i].score_C);
		fscanf(pf,"%d", &ps->elem[i].score_S);
	}
}

void Search_name(SSTable* ps)
{
    
    
	int i = 0;
	char name[50];
	printf("请输入要查找的姓名->");
	scanf("%s", name);
	for (i = 0; i < ps->size; i++)
	{
    
    
		if (strcmp(name, ps->elem[i].name) == 0)
		{
    
    
			printf("%d", ps->elem[i].num);
			printf("%5s", ps->elem[i].name);
			printf("%5d", ps->elem[i].score_G);
			printf("%5d", ps->elem[i].score_C);
			printf("%5d", ps->elem[i].score_S);
			break;
		}
		else
		{
    
    
			printf("无相关信息!");
		}
	}
}

void Search_score(SSTable* ps)
{
    
    
	int score_S;
	printf("\n请输入要查找的数据结构成绩:\n");
	scanf("%d", &score_S);
	int low = 0;
	int high = ps->size - 1;
	int mid = (low + high) / 2;
	while (low <= high)
	{
    
    
		mid = (low + high) / 2;

		if (score_S == ps->elem[mid].score_S)
		{
    
    
			printf("%4d", ps->elem[mid].num);
			printf("%5s", ps->elem[mid].name);
			printf("%5d", ps->elem[mid].score_G);
			printf("%5d", ps->elem[mid].score_C);
			printf("%5d", ps->elem[mid].score_S);
			break;
		}
		else if (score_S < ps->elem[mid].score_S)
		{
    
    
			high = mid - 1;
		}
		else
		{
    
    
			low = mid + 1;
		}

	}
	if (high < low)
	{
    
    
		printf("无相关信息!");
	}
}

void sort_score_S(SSTable* ps)
{
    
    
	int i, j;
	SLDataType temp;
	for (i = 0; i < ps->size - 1; i++)
	{
    
    
		for (j = 0; j < ps->size - 1 - i; j++)
		{
    
    
			if (ps->elem[j].score_S > ps->elem[j+1].score_S)
			{
    
    
				temp = ps->elem[j+1];
				ps->elem[j+1] = ps->elem[j];
				ps->elem[j] = temp;
			}
		}
	}
	printf("\n将数据结构成绩按由小到大进行排序:\n");
	for (i = 0; i < ps->size; i++)
	{
    
    
		printf("%5d", ps->elem[i].num);
		printf("%10s", ps->elem[i].name);
		printf("%5d", ps->elem[i].score_G);
		printf("%5d", ps->elem[i].score_C);
		printf("%5d", ps->elem[i].score_S);
		printf("\n");
	}
}

void menu()
{
    
    
	printf("\n---------------1.按姓名查找---------------\n");
	printf("\n-----------2.按数据结构成绩查找------------\n");
	printf("\n---------------0.退出查找!---------------\n");
}

int main()
{
    
    
	SSTable s;
	SeqListInit(&s);
	int input = 0;
	menu();
	CreateList(&s);
	sort_score_S(&s);
	do
	{
    
    
		menu();
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			Search_name(&s);
			break;
		case 2:
			Search_score(&s);
			break;
		case 0:
			break;
		default:
			printf("该关键字非法!");
			break;
		}
	} while (input);
	return 0;
}


实现给定数组的快速排序

题目描述

内容要求:

  1. 以菜单的形式作为用户界面,接收待排序的输入序列,使用快速排序的方法对输入序列进行快速排序。
  2. 测试数据:
    80,43,18,21,30,13,52,51,75

题目分析

快速排序是通过递归或非递归实现的,其中对于单趟PartSort也有三种不同的算法,这三种不同的算法效率没有差异,通常是通过递归实现快速排序,非递归需要借助栈或队列,这里展示的是递归版、前后指针法实现快速排序,如果有其他需求可以看此文章自行寻找所需算法

数据结构—手撕图解排序(含动图演示)

#include <stdio.h>
#include <stdlib.h>

void PrintArrey(int* a, int n)
{
    
    
	for (int i = 0; i < n; i++)
	{
    
    
		printf("%d ", a[i]);
	}
	printf("\n");
}

void Swap(int* a, int* b)
{
    
    
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

int PartSort(int* a, int left, int right)
{
    
    
	int cur = left + 1;
	int prev = left;
	int keyi = left;
	while (cur <= right)
	{
    
    
		if (a[cur] < a[keyi])
		{
    
    
			++prev;
			Swap(&a[prev], &a[cur]);
		}
		cur++;
	}

	Swap(&a[prev], &a[keyi]);

	return prev;
}


void QuickSort(int* a, int begin, int end)
{
    
    
	if (begin >= end)
	{
    
    
		return;
	}
	int keyi = PartSort(a, begin, end);

	QuickSort(a, begin, keyi - 1);
	QuickSort(a, keyi + 1, end);
}

void menu()
{
    
    
	printf("*****************************\n");
	printf("********* 1. Sort **********\n");
	printf("********* 0. exit  **********\n");
	printf("*****************************\n");
}

void CheckCapacity(int* a, int num, int* capacity)
{
    
    
	if (num > *capacity)
	{
    
    
		int* tmp = (int*)realloc(a, sizeof(int) * num);
		if (tmp == NULL)
		{
    
    
			perror("realloc fail");
			return;
		}
		a = tmp;
		*capacity = num;
	}
	else
	{
    
    
		return;
	}
}

void Sort()
{
    
    
	int capacity = 10;
	int* a = (int*)malloc(sizeof(int) * capacity);
	if (a == NULL)
	{
    
    
		perror("malloc fail");
		return;
	}
	int num;
	printf("输入要输入元素的个数->");
	scanf("%d", &num);
	CheckCapacity(a, num, &capacity);
	printf("输入对应个数的元素->");
	for (int i = 0; i < num; i++)
	{
    
    
		scanf("%d", &a[i]);
	}
	QuickSort(a, 0, num - 1);
	printf("排序完成:\n");
	PrintArrey(a, num);
	free(a);
	a = NULL;
}

int main()
{
    
    
	int input = 0;
	do
	{
    
    
		menu();
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			Sort();
			break;
		case 0:
			break;
		default:
			printf("输入错误 重新输入\n");
		}
	} while (input);
}

猜你喜欢

转载自blog.csdn.net/qq_73899585/article/details/131840817
今日推荐