C language---data structure experiment---realization of search algorithm---realize quick sorting of a given array

written in front

  1. This article is also implemented through dynamic memory

  2. Quick sorting is implemented through recursion or non-recursion. There are also three different algorithms for a single PartSort. There is no difference in the efficiency of these three different algorithms. Usually, quick sorting is achieved through recursion. Non-recursion requires the use of stacks or queues. Here The display shows the recursive version and the forward and backward pointer method to achieve quick sorting. If you have other needs, you can read this article to find the required algorithm by yourself.

Data Structure - Hand-Torn Diagram Sorting (Including GIF Demonstration)

Find the implementation of the algorithm

topic description

Content requirements:

  1. Create a lookup table like this:

Scholarship name Higher Mathematics C Program Design Data Structure
1301 White Snow 88 92 89
1302 Changliang 73 77 68
1303 Feng Ling 80 82 75
1304 Li Bin 90 89
90 1305 Renfang 60 71 58 130
78
88 1307 Wu Wei 95 89 90
1308 Yang Ning 75 86 84
1309 Zhang Hua 83 79 80
1310 Zheng Xin 58 62 60

  1. Using the sequential lookup algorithm, find students named Ren Fang and Zhao Bin from the lookup table. If the search is successful, the relevant information of the student will be given, and if the search is unsuccessful, the corresponding prompt information will be given.

  2. Using the binary lookup algorithm, find the students with grades 68 and 90 in the data structure from the lookup table. If the search is successful, the relevant information of the student will be given, and if the search is unsuccessful, the corresponding prompt information will be given.

topic analysis

Manual data entry method

#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;
}

read file directly

To read the file directly, you need to add the content of test.txt in the root directory
as the table content

#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;
}


implements a quick sort of the given array

topic description

Content requirements:

  1. Use the form of menu as the user interface to receive the input sequence to be sorted, and use the quick sort method to quickly sort the input sequence.
  2. Test data:
    80, 43, 18, 21 , 30, 13, 52, 51, 75

topic analysis

Quick sorting is implemented through recursion or non-recursion. There are also three different algorithms for a single PartSort. There is no difference in the efficiency of these three different algorithms. Usually, quick sorting is achieved through recursion. Non-recursion requires the use of stacks or queues. Here The display shows the recursive version and the forward and backward pointer method to achieve quick sorting. If you have other needs, you can read this article to find the required algorithm by yourself.

Data Structure - Hand-Torn Diagram Sorting (Including GIF Demonstration)

#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);
}

Guess you like

Origin blog.csdn.net/qq_73899585/article/details/131840817