自己创建网站教程,怎么去掉2345网址导航,深圳企业500强,陕西省建设厅网站怎么查焊工证1.qsort函数
1.1qsort函数的介绍 资源来源于cplusplus网站 1.2qsort函数的主要功能 对数组的元素进行排序 对数组中由 指向的元素进行排序#xff0c;每个元素字节长#xff0c;使用该函数确定顺序。 此函数使用的排序算法通过调用指定的函数来比较元素对#xff0c;并将指…1.qsort函数
1.1qsort函数的介绍 资源来源于cplusplus网站 1.2qsort函数的主要功能 对数组的元素进行排序 对数组中由 指向的元素进行排序每个元素字节长使用该函数确定顺序。 此函数使用的排序算法通过调用指定的函数来比较元素对并将指向它们的指针作为参数。 该函数不返回任何值但通过重新排序数组的元素如 所定义来修改指向的数组的内容。 等效元素的顺序未定义。 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*)basej*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函数测试整型数据
#includestdiio.h
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*)basej*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;
}2.4bubble_sort函数测试结构体数据
#includestdio.h
#includestring.h
//实现交换的函数
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头文件string.h)
//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*)basej*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.用名字比较 2.用年龄比较