【利用冒泡排序的思想模拟实现qsort函数】

1.qsort函数

1.1qsort函数的介绍

资源来源于cplusplus网站
【利用冒泡排序的思想模拟实现qsort函数】_第1张图片

1.2qsort函数的主要功能

【利用冒泡排序的思想模拟实现qsort函数】_第2张图片

对数组的元素进行排序
对数组中由 指向的元素进行排序,每个元素字节长,使用该函数确定顺序。
此函数使用的排序算法通过调用指定的函数来比较元素对,并将指向它们的指针作为参数。
该函数不返回任何值,但通过重新排序数组的元素(如 所定义)来修改指向的数组的内容。
等效元素的顺序未定义。

void qsort (void* base, size_t num, size_t size,
            int (*compar)(const void*,const void*));

qsort函数有4个参数,第一个是需要比较的元素的地址,第二个是比较的元素的个数,第三个是比较的元素的大小(单位字节),第四个是自定义比较函数的地址(这个是需要使用者自己根据数据类型自己实现的)

2.函数的实现

2.1主要函数bubble_sort的实现

void bubble_sort(void* base,size_t sz,size_t width,int (*cmp)(const void* e1,const void* e2))
{
	//趟数
	int i = 0;
	for (i = 0; i < sz-1; i++)
	{
		//一趟冒泡排序
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (cmp((char*)base+j*width, (char*)base + (j + 1)*width) > 0)
			{
			//交换
				Swap((char*)base + j*width, (char*)base + (j + 1)*width,width);//width是比较元素的宽度
			}
		}
	}
}

2.2交换函数Swap函数的实现

void Swap(char* buf1, char* buf2, size_t width)
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
		char tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}

2.3bubble_sort函数测试整型数据

#include
void Swap(char* buf1, char* buf2, size_t width)
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
		char tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}
//自定义比较的函数(以整型为例)
int cmp_int(const void* e1, const void* e2)
{
	return *(int*)e1 - *(int*)e2;
}
void bubble_sort(void* base,size_t sz,size_t width,int (*cmp)(const void* e1,const void* e2))
{
	//趟数
	int i = 0;
	for (i = 0; i < sz-1; i++)
	{
		//一趟冒泡排序
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (cmp((char*)base+j*width, (char*)base + (j + 1)*width) > 0)
			{
				Swap((char*)base + j*width, (char*)base + (j + 1)*width,width);
			}
		}
	}
}
//自定义打印函数(整型打印)
void print1(int* arr,size_t sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
}
//bubble_sort函数测试整型数据
void Test1()
{
	int arr[] = { 3,5,2,4,7,8,6,9,0,1 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int width = sizeof(arr[0]);
	bubble_sort(arr, sz, width, cmp_int);
	print(arr, sz);
}
int main()
{
	Test1();
	return 0;
}

【利用冒泡排序的思想模拟实现qsort函数】_第3张图片

2.4bubble_sort函数测试结构体数据

#include
#include
//实现交换的函数
void Swap(char* buf1, char* buf2, size_t width)
{
	int i = 0;
	for (i = 0; i < width; i++)
	{
		char tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}
//定义结构体类型
struct S
{
	char name[20];
	int age;
};

//自定义比较函数(结构体数据)
//1.用名字比较(需要用到字符串比较函数strcmp,头文件)
//int cmp_stu_by_name(const void* e1, const void* e2)
//{
	//return strcmp(((struct S*)e1)->name, ((struct S*)e2)->name);
//}
//2.用年龄比较
int cmp_stu_by_age(const void* e1, const void* e2)
{
	return ((struct S*)e1)->age - ((struct S*)e2)->age;
}
void bubble_sort(void* base,size_t sz,size_t width,int (*cmp)(const void* e1,const void* e2))
{
	//趟数
	int i = 0;
	for (i = 0; i < sz-1; i++)
	{
		//一趟冒泡排序
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (cmp((char*)base+j*width, (char*)base + (j + 1)*width) > 0)
			{
				Swap((char*)base + j*width, (char*)base + (j + 1)*width,width);
			}
		}
	}
}
void print2(struct S* arr, size_t sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s %d\n", arr[i].name, arr[i].age);
	}
}

//bubble_sort函数测试结构体数据
void Test2()
{
	struct S arr2[] = { {"zhangsan",27},{"lisi",35},{"wamgwu",31}};
	int sz = sizeof(arr2) / sizeof(arr2[0]);
	int width = sizeof(arr2[0]);
	//bubble_sort(arr2, sz, width, cmp_stu_by_name);//以名字排序
	bubble_sort(arr2, sz, width, cmp_stu_by_age);//以年龄排序
	print2(arr2, sz);
}
int main()
{
	Test2();
	return 0;
}

1.用名字比较

【利用冒泡排序的思想模拟实现qsort函数】_第4张图片

2.用年龄比较

【利用冒泡排序的思想模拟实现qsort函数】_第5张图片

你可能感兴趣的:(C语言,#,函数,#冒泡排序,c语言,函数模拟实现,算法)