C-库函数 qsort 的使用及模拟实现

这篇博客详细介绍了冒泡排序和快速排序两种经典的排序算法。首先,通过冒泡排序的实现,展示了如何对整型数组进行排序,并提供了自定义比较函数以适应不同类型的排序需求。接着,利用C标准库的qsort函数,实现了对整型、字符型和结构体数组的排序。此外,还模拟实现了qsort功能的bubble_qsort函数,同样支持多种类型排序。博客最后通过示例测试了各种排序功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

struct Stu
{
	char name[20];
	int age;
};
// 冒泡排序 -> 不能对任意类型进行排序
void bubble_sort(int *p,int s)
{
	// 趟数
	for (int i = 0; i < s - 1; i++)
	{
		// 每趟
		for (int j = 0; j < s - i - 1; j++)
		{
			// 若需对其他类型进行排序,仅比较和交换部分需要修改
			if (*(p + j) > *(p + j + 1))
			{
				// 交换
				int temp = *(p + j);
				*(p + j) = *(p + j + 1);
				*(p + j + 1) = temp;
			}
		}
	}
}

void print_arr(int* p, int s)
{
	for (int i = 0; i < s; i++)
	{
		printf("%d ", *(p+i));
	}
	printf("\n");
}

// void qsort(void* base,			// base中存放的是待排序数据中第一个对象的地址
//	          size_t num,			// 排序数据元素的个数
//			  size_t size,			// 排序数据中一个元素的大小,单位是字节
//			  int (*cmp)(const void* el, const void* e2)	);	// 是用来比较待排序数据中的2个相邻元素的函数,由用户自定义

int cmp_int(const void* e1, const void* e2)
{
	return *((int*)e1) - *((int*)e2);
}
int cmp_char(const void* e1, const void* e2)
{
	return strcmp((char*)e1, (char*)e2);  // <string.h> 比较字符串,使用库函数 strcmp,本质比较的是ASCII码的大小
}
int sort_by_name(const void* e1, const void* e2)
{
	return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);  // <string.h>
}
int sort_by_age(const void* e1, const void* e2)
{
	return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}

void test_int_sort()
{
	int arr[10] = { 2,4,5,7,6,8,9,1,10,3 };
	int length = sizeof(arr) / sizeof(arr[0]);

	//bubble_sort(arr, length);
	//print_arr(arr, length);

	// 用库函数 qsort (需引用头文件<stdlib.h>)
	qsort(arr, length, sizeof(arr[0]), cmp_int);
	print_arr(arr, length);
}
void test_char_sort()
{
	char ch[] = { 'f','a','m','i' };
	int length = sizeof(ch) / sizeof(ch[0]);
	qsort(ch, length, sizeof(ch[0]), cmp_char);
	//print_arr(ch, length); //不能这么写
}

void test_struct_sort()
{
	struct Stu stu[3] = { {"zhangsan",35},{"lisi",40},{"wangwu",30}};
	int length = sizeof(stu) / sizeof(stu[0]);
	qsort(stu, length, sizeof(stu[0]), sort_by_name);
	qsort(stu, length, sizeof(stu[0]), sort_by_age);
}

// 模拟实现 qsort 函数

// 交换函数:逐字节进行交换!
void Swap(char* buf1, char* buf2, int width)
{
	for (int i = 0; i < width; i++)
	{
		char temp = *buf1;
		*buf1 = *buf2;
		*buf2 = temp;
		buf1++;
		buf2++;
	}
}

void bubble_qsort(void* base, int length, int width, int (*cmp)(const void* e1, const void* e2))
{
	// 趟数
	for (int i = 0; i < length - 1; i++)
	{
		// 每趟
		for (int j = 0; j < length - i - 1; 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 test_int_bubble_qsort()
{
	int arr[10] = { 2,4,5,7,6,8,9,1,10,3 };
	int length = sizeof(arr) / sizeof(arr[0]);

	bubble_qsort(arr, length, sizeof(arr[0]), cmp_int);
	print_arr(arr, length);
}
void test_char_bubble_qsort()
{
	char ch[] = { 'f','a','m','i' };
	int length = sizeof(ch) / sizeof(ch[0]);
	bubble_qsort(ch, length, sizeof(ch[0]), cmp_char);
}
void test_struct_bubble_qsort()
{
	struct Stu stu[3] = { {"zhangsan",35},{"lisi",40},{"wangwu",30} };
	int length = sizeof(stu) / sizeof(stu[0]);
	bubble_qsort(stu, length, sizeof(stu[0]), sort_by_name);
	bubble_qsort(stu, length, sizeof(stu[0]), sort_by_age);
}
int main()
{
	// 测试库函数 qsort
	//test_int_sort();
	//test_char_sort();
	//test_struct_sort();
	
	// 测试模拟库函数 qsort 实现的 bubble_qsort
	//test_int_bubble_qsort();
	//test_char_bubble_qsort();
	test_struct_bubble_qsort();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值