c语言库函数qsort

#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define _CRT_SECURE_NO_WARNINGS 1
void bubble_sort(int arr[], int sz);
void Swap(char* b1, char* b2, int width);
/*int add(int x, int y);
void print(char*str);
int main() {
	//int (*p)(int, int) = add;
	//printf("%d", (*p)(2, 3));
	void (*p)(char*) = print;
	(*p)('dasdas');

	return 0;
}
int add(int x,int y) {
	return x + y;

}
void print(char*str)
{
	printf("%s", str);
}*/
//void print(int(*p)[5], int x, int y);
void qsort(void* base, size_t num, size_t width, int (*cmp)(const void* e1, const void* w2));
int cmp_int(const void* e1, const void* e2);
int cmp_float(const void* e1, const void* e2);
int cmp_stu_by_age(const void* e1, const void* e2);
void bubble(void* base, int sz, int width, int (*cmp)(void*e1, void*e2));
struct Stu
{
	char name[20];
	int age;
};
void test1();
int
main(){
	int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);

	bubble(arr,sz,sizeof(arr[0]),cmp_int);
	struct Stu s[3] = { {"sadas",23}, {"324",32}, {"324",343} };
	float f[] = { 1.0,2.0,5.0,3.0 };
	int sz1 = sizeof(f) / sizeof(f[0]);
	//qsort(arr, sz, sizeof(arr[0]), cmp_int);
	qsort(f, sz1, sizeof(f[0]), cmp_float);
	test1();
	int i = 0;
	for ( i = 0; i < sz; i++)
	{
		printf("%d", arr[i]);
		//printf("%f", f[i]);

	}
	int j=0;
	for (j = 0; j < sz1; j++)
	{
		printf("%f", f[j]);
		//printf("%f", f[i]);

	}
	return 0;
}
//void 类型可以接受任意类型的指针
//不能进行解引用操作
//不能进行+-
void bubble_sort(int arr[], int sz) {
	int i = 0;
	for ( i = 0; i < sz-1; i++)
	{
		int j = 0;
		for ( j = 0; j < sz-1-i; j++)
		{
			if (arr[j]>arr[j+1])
			{
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;

			}
		}

	}
}
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);
}
void test1()
{
	struct Stu s[3] = { {"sadas",23}, {"324",32}, {"324",343} };
	int sz = sizeof(s) / sizeof(s[0]);
	qsort(s, sz, sizeof(s[0]), cmp_stu_by_age);

}
int cmp_stu_by_age(const void* e1, const void* e2)
{
	return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;

}
int cmp_stu_by_name(const void* e1, const void* e2)
{
	return strcmp(((struct Stu*)e1)->name , ((struct Stu*)e2)->name);

}
void bubble(void* base, int sz, int 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 Swap(char* b1, char* b2, int width) {
	int i = 0;
	for ( i = 0; i <width; i++)
	{
		char tmp = *b1;
		*b1 = *b2;
		*b2 = tmp;
		b1++;
		b2++;

	}


}
//1.元素地址
//2.元素个数
//2.每个大小
//4.比较两个函数所用的函数地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值