This is the 26th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

Qsort library functions sort

Quick sort Quick sortOriginal function interpretation

image-20210818230558340


With the tree

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

struct Stu
{
	char name[20];
	int age;
};

int cmp_int(const void* e1, const void* e2)
{
	return* (int*)e1 - *(int*)e2;
}

int cmp_float(const void* e1, const void* e2)
{
	return (int) (* (float*)e1 - *(float*)e2);
}

// There are different ways to sort structures. Each person has a different standard. Some people are names, some people are names
int cmp_stu_age(const void* e1, const void* e2)
{
	return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}

int cmp_stu_name(const void* e1, const void* e2)
{
	// Name comparison is comparing strings
	// String comparisons cannot be directly compared using <>=, instead using STRCMP

	return strcmp(((struct Stu*)e1)->name , ((struct Stu*)e2)->name);
}

void test_int(a)
{
	int arr[10] = { 0.5.6.3.5.9.6.5.3.9 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_int);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%2d ", arr[i]);
	}
	printf("\n");
}
void test_float(a)
{
	float arr[10] = { 0.0.5.0.6.0.3.0.5.1.9.0.6.3.5.6.3.3.9.5 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_float);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%f ", arr[i]);
	}
	printf("\n");
}
void test_stu_age(a)
{
	struct Stu s[3] ={{"zhuangsan".20}, {"lisi".21}, {"wangwu".10}};int sz = sizeof(s) / sizeof(s[0]);
	qsort(s, sz, sizeof(s[0]), cmp_stu_age);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s ", (&s[i])->name);
		printf("%d ", (&s[i])->age);
	}
	printf("\n");
}
void test_stu_name(a)
{
	struct Stu s[3] ={{"zhuangsan".20}, {"lisi".21}, {"wangwu".10}};int sz = sizeof(s) / sizeof(s[0]);
	qsort(s, sz, sizeof(s[0]), cmp_stu_name);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s ", (&s[i])->name);
		printf("%d ", (&s[i])->age);
	}
	printf("\n");
}
int main(a)
{
	test_int();
	test_float();
	test_stu_age();
	test_stu_name();
	return 0;
}
Copy the code
image-20210818225357880


You’d probably want to write your own qsort at this point, so we’ll copy it as BubbleSort

image-20210831222802853


Char * / void void/char* / width

image-20210831222830598


image-20210831223052692


Use the BubbleSort

#include<stdio.h>
struct Stu
{
	char name[20];
	int age;
};

// Write your own universal exchange function
void Swap(char* buf1, char* buf2,int width)
{
	size_t i = 0;
	for (i = 0; i < width; i++)
	{
		chartmp = *buf1; *buf1 = *buf2; *buf2 = tmp; buf1++; buf2++; }}// Write a bubble function of your own, using the callback function to implement a generic bubble sort function
voidBubbleSort(void* base, int num, size_t width, int (*cmp)(const void* e1, const void* e2))
{
	int i = 0;
	/ / number
	for (i = 0; i < num - 1; i++)
	{
		// The logarithm of the comparison
		int j = 0;
		for (j = 0; j < num - 1 - i; j++)
		{
			if (cmp((char*)base+j*width,(char*)base+(j+1)*width) > 0)
			{
				/ / exchange
				Swap((char*)base + j * width, (char*)base + (j + 1) * width,width); }}}}int cmp_int(const void* e1, const void* e2)
{
	return* (int*)e1 - *(int*)e2;
}

int cmp_float(const void* e1, const void* e2)
{
	return (int) (* (float*)e1 - *(float*)e2);
}

int cmp_stu_age(const void* e1, const void* e2)
{
	return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
int cmp_stu_name(const void* e1, const void* e2)
{
	// Name comparison is comparing strings
	// String comparisons cannot be directly compared using <>=, instead using STRCMP

	return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}
void test_int(a)
{
	int arr[10] = { 0.5.6.3.5.9.6.5.3.9 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	BubbleSort(arr, sz, sizeof(arr[0]), cmp_int);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%2d ", arr[i]);
	}
	printf("\n");
}
void test_float(a)
{
	float arr[10] = { 0.0.5.0.6.0.3.0.5.1.9.0.6.3.5.6.3.3.9.5 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	BubbleSort(arr, sz, sizeof(arr[0]), cmp_float);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%f ", arr[i]);
	}
	printf("\n");
}
void test_stu_age(a)
{
	struct Stu s[3] ={{"zhuangsan".20}, {"lisi".21}, {"wangwu".10}};int sz = sizeof(s) / sizeof(s[0]);
	BubbleSort(s, sz, sizeof(s[0]), cmp_stu_age);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s ", (&s[i])->name);
		printf("%d ", (&s[i])->age);
	}
	printf("\n");
}

void test_stu_name(a)
{
	struct Stu s[3] ={{"zhuangsan".20}, {"lisi".21}, {"wangwu".10}};int sz = sizeof(s) / sizeof(s[0]);
	BubbleSort(s, sz, sizeof(s[0]), cmp_stu_name);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s ", (&s[i])->name);
		printf("%d ", (&s[i])->age);
	}
	printf("\n");
}
int main(a)
{
	test_int();
	test_float();
	test_stu_age();
	test_stu_name();
	return 0;
}
Copy the code
image-20210831223655636